#justcommonthingz
This commit is contained in:
parent
ff16a2e12e
commit
57029ed818
40
README.md
40
README.md
|
@ -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
|
||||
|
|
|
@ -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
|
||||
```
|
|
@ -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)
|
||||
}
|
|
@ -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.
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# squish
|
||||
--
|
||||
import "git.tcp.direct/kayos/common/squish"
|
||||
|
||||
`import "git.tcp.direct/kayos/common/squish"`
|
||||
|
||||
## Usage
|
||||
|
||||
|
|
19
util.go
19
util.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue