#justcommonthingz

This commit is contained in:
kayos@tcp.direct 2022-02-08 04:21:57 -08:00
parent ff16a2e12e
commit 57029ed818
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
8 changed files with 115 additions and 38 deletions

View File

@ -1,8 +1,20 @@
# common
--
import "git.tcp.direct/kayos/common"
stuff I use a lot.
Welcome to things. Here are some of the aforementioned:
* [hash](./hash)
* [linux](./linux)
* [squish](./squish)
* [entropy](./entropy)
## base
`import "git.tcp.direct/kayos/common"`
### Usage
#### func Abs
@ -11,22 +23,6 @@ func Abs(n int) int
```
Abs will give you the positive version of a negative integer, quickly.
#### func BytesToBlake2b
```go
func BytesToBlake2b(b []byte) []byte
```
BytesToBlake2b ignores all errors and gives you a blakae2b 64 hash value as a
byte slice. (or panics somehow)
#### func CompareChecksums
```go
func CompareChecksums(a []byte, b []byte) bool
```
CompareChecksums will take in two byte slices, hash them with blake2b, and tell
you if the resulting checksums match.
#### func Fprint
```go
@ -34,6 +30,12 @@ func Fprint(w io.Writer, s string)
```
Fprint is fmt.Fprint with error handling.
#### func OneInA
```go
func OneInA(million int) bool
```
#### func RNG
```go

18
entropy/README.md Normal file
View File

@ -0,0 +1,18 @@
# entropy
`import "git.tcp.direct/kayos/common/entropy"`
## Usage
#### func RNG
```go
func RNG(n int) int
```
#### func RandomStrChoice
```go
func RandomStrChoice(choice []string) string
```

29
entropy/entropy.go Normal file
View File

@ -0,0 +1,29 @@
package entropy
import (
crip "crypto/rand"
"encoding/binary"
"math/rand"
"time"
)
func RandomStrChoice(choice []string) string {
strlen := len(choice)
n := uint32(0)
if strlen > 0 {
n = getRandomUint32() % uint32(strlen)
}
return choice[n]
}
func getRandomUint32() uint32 {
x := time.Now().UnixNano()
return uint32((x >> 32) ^ x)
}
func RNG(n int) int {
var seed int64
binary.Read(crip.Reader, binary.BigEndian, &seed)
rng := rand.New(rand.NewSource(seed))
return rng.Intn(n)
}

22
hash/README.md Normal file
View File

@ -0,0 +1,22 @@
# hash
`import "git.tcp.direct/kayos/common/hash"`
## Usage
#### func Blake2bSum
```go
func Blake2bSum(b []byte) []byte
```
Blake2bSum ignores all errors and gives you a blakae2b 64 hash value as a byte
slice. (or panics somehow)
#### func BlakeEqual
```go
func BlakeEqual(a []byte, b []byte) bool
```
BlakeEqual will take in two byte slices, hash them with blake2b, and tell you if
the resulting checksums match.

17
hash/hash.go Normal file
View File

@ -0,0 +1,17 @@
package hash
import "golang.org/x/crypto/blake2b"
// Blake2bSum ignores all errors and gives you a blakae2b 64 hash value as a byte slice. (or panics somehow)
func Blake2bSum(b []byte) []byte {
Hasha, _ := blake2b.New(64, nil)
Hasha.Write(b)
return Hasha.Sum(nil)
}
// BlakeEqual will take in two byte slices, hash them with blake2b, and tell you if the resulting checksums match.
func BlakeEqual(a []byte, b []byte) bool {
ahash := Blake2bSum(a)
bhash := Blake2bSum(b)
return string(ahash) == string(bhash)
}

View File

@ -39,10 +39,10 @@ const (
UnameRelease
// UnameDomain is "domainname", the kernel domain name.
UnameDomain
// UnameVersion is "version", or "uname -v".
UnameVersion
// UnameHostname is "Nodename" or "uname -n"
UnameHostname
// UnameVersion is "version", or "uname -v".
UnameVersion
)
// GetUname uses system calls to retrieve the same values as the uname linux command

View File

@ -1,6 +1,6 @@
# squish
--
import "git.tcp.direct/kayos/common/squish"
`import "git.tcp.direct/kayos/common/squish"`
## Usage

19
util.go
View File

@ -9,7 +9,6 @@ import (
"time"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/blake2b"
)
const charset = "abcdefghijklmnopqrstuvwxyz1234567890"
@ -22,20 +21,6 @@ func Fprint(w io.Writer, s string) {
}
}
// BytesToBlake2b ignores all errors and gives you a blakae2b 64 hash value as a byte slice. (or panics somehow)
func BytesToBlake2b(b []byte) []byte {
Hasha, _ := blake2b.New(64, nil)
Hasha.Write(b)
return Hasha.Sum(nil)
}
// CompareChecksums will take in two byte slices, hash them with blake2b, and tell you if the resulting checksums match.
func CompareChecksums(a []byte, b []byte) bool {
ahash := BytesToBlake2b(a)
bhash := BytesToBlake2b(b)
return string(ahash) == string(bhash)
}
// RNG is a Random Number Generator
func RNG(n int) int {
var seed int64
@ -69,3 +54,7 @@ func Abs(n int) int {
n64 = (n64 ^ y) - y
return int(n64)
}
func OneInA(million int) bool {
return RNG(million) == 1
}