Compare commits

..

2 Commits

9 changed files with 196 additions and 208 deletions

@ -42,7 +42,7 @@ jobs:
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
@ -53,7 +53,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v3
uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
@ -67,4 +67,4 @@ jobs:
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
uses: github/codeql-action/analyze@v2

@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-go@v5
- uses: actions/setup-go@v4
with:
go-version: '1.18'
- name: Run coverage

@ -2,12 +2,67 @@ package common
import (
"errors"
"fmt"
"io"
"os"
"sync"
"testing"
"git.tcp.direct/kayos/common/entropy"
"git.tcp.direct/kayos/common/hash"
"git.tcp.direct/kayos/common/squish"
)
var needle = []byte(entropy.RandStr(16))
func TestBlakeEqualAndB64(t *testing.T) {
var clone = make([]byte, len(needle))
copy(clone, needle)
if !hash.BlakeEqual(needle, clone) {
t.Fatalf("BlakeEqual failed! Values %v and %v should have been equal.\n|---->Lengths: %d and %d",
needle, clone, len(needle), len(clone),
)
}
falseclone := []byte(entropy.RandStr(16))
if hash.BlakeEqual(needle, falseclone) {
t.Fatalf("BlakeEqual failed! Values %v and %v should NOT have been equal.\n|---->Lengths: %d and %d",
needle, clone, len(needle), len(clone),
)
}
var based = [2][]byte{needle, clone}
based[0] = []byte(squish.B64e(based[0]))
based[1] = []byte(squish.B64e(based[0]))
if hash.BlakeEqual(based[0], based[1]) {
t.Fatalf("Base64 encoding failed! Values %v and %v should NOT have been equal.\n|---->Lengths: %d and %d",
based[0], based[1], len(based[0]), len(based[1]),
)
}
// sneakin in some code coverage rq dwai nbd
bogusRd, bogusWrt := io.Pipe()
bogusRd2, bogusWrt2 := io.Pipe()
t.Logf("\n")
go func() {
Fprint(io.MultiWriter(bogusWrt, os.Stdout), fmt.Sprintf("[PASS] based[0] = %s", string(based[0])))
Fprintf(io.MultiWriter(bogusWrt2, os.Stdout), "\n[PASS] based[1] = %s", string(based[0]))
}()
_ = bogusWrt.CloseWithError(io.ErrClosedPipe)
_ = bogusWrt2.CloseWithError(io.ErrClosedPipe)
_, err := bogusRd.Read([]byte{})
if err == nil {
t.Fatalf("should have been an error...")
}
_, err = bogusRd2.Read([]byte{})
if err == nil {
t.Fatalf("should have been an error...")
}
}
func TestAbs(t *testing.T) {
var start = int32(entropy.RNG(5))
for start < 1 {

@ -4,8 +4,11 @@ import (
crip "crypto/rand"
"encoding/binary"
"math/rand"
"os"
"reflect"
"sync"
"time"
"unsafe"
"nullprogram.com/x/rng"
@ -16,11 +19,11 @@ type randPool struct {
sync.Pool
}
func (p *randPool) Get() *rand.Rand {
return p.Pool.Get().(*rand.Rand)
func (p *randPool) Get() *rng.SplitMix64 {
return p.Pool.Get().(*rng.SplitMix64)
}
func (p *randPool) Put(r *rand.Rand) {
func (p *randPool) Put(r *rng.SplitMix64) {
p.Pool.Put(r)
}
@ -30,8 +33,7 @@ var (
New: func() interface{} {
sm64 := new(rng.SplitMix64)
sm64.Seed(GetCryptoSeed())
prng := rand.New(sm64) //nolint:gosec
return prng
return sm64
},
},
}
@ -42,16 +44,18 @@ var (
func setSharedRand() {
hardLocc.Lock()
sharedRand = lolXD.Get()
sharedRand = rand.New(lolXD.Get())
hardLocc.Unlock()
}
func AcquireRand() *rand.Rand {
return lolXD.Get()
return rand.New(lolXD.Get())
}
func ReleaseRand(r *rand.Rand) {
lolXD.Put(r)
srcField := reflect.ValueOf(r).Elem().FieldByName("src")
src := reflect.NewAt(srcField.Type(), unsafe.Pointer(srcField.UnsafeAddr())).Elem().Interface().(*rng.SplitMix64)
lolXD.Put(src)
r = nil
}
@ -80,18 +84,17 @@ func GetOptimizedRand() *rand.Rand {
}
// GetSharedRand returns a pointer to our shared optimized rand.Rand which uses crypto/rand to seed a splitmix64 rng.
// WARNING - RACY - This is not thread safe, and should only be used in a single-threaded context.
func GetSharedRand() *rand.Rand {
getSharedRand.Do(func() {
setSharedRand()
})
return sharedRand
return rand.New(sharedRand)
}
// RNGUint32 returns a random uint32 using crypto/rand and splitmix64.
func RNGUint32() uint32 {
r := lolXD.Get()
ui := r.Uint32()
ui := uint32(r.Int63() >> 31)
lolXD.Put(r)
return ui
}
@ -106,8 +109,25 @@ RNG returns integer with a maximum amount of 'n' using a global/shared instance
*/
func RNG(n int) int {
r := lolXD.Get()
i := r.Intn(n)
lolXD.Put(r)
defer lolXD.Put(r)
i := 0
if n <= 0 {
// because panic is just rude.
_, _ = os.Stderr.WriteString("RNG: n must be greater than 0, returning 0")
return i
}
if n <= 1<<31-1 {
n32 := int32(n)
if n32&(n32-1) == 0 {
i = int(int32(r.Int63()>>32) & (n32 - 1))
}
maximum := int32((1 << 31) - 1 - (1<<31)%uint32(n32))
v := int32(r.Int63() >> 32)
for v > maximum {
v = int32(r.Int63() >> 32)
}
i = int(v % n32)
}
return i
}
@ -167,7 +187,7 @@ func randStr(upper bool, size int) string {
buf := strBufs.Get()
r := lolXD.Get()
for i := 0; i != size; i++ {
ui32 := int(r.Uint32())
ui32 := int(r.Int63() >> 31)
switch upper {
case true:
_ = buf.WriteByte(charsetWithUpper[ui32%len(charsetWithUpper)])

@ -49,15 +49,15 @@ func Test_RNG(t *testing.T) {
t.Errorf("GetSharedRand(55555) returned the same value twice!")
}
r := AcquireRand()
one := r.Intn(55555)
two := r.Intn(55555)
one := r.Uint64()
two := r.Uint64()
if one == two {
t.Errorf("AcquireRand() returned the same value twice!")
}
ReleaseRand(r)
r = AcquireRand()
one1 := r.Intn(55555)
two1 := r.Intn(55555)
one1 := r.Uint64()
two1 := r.Uint64()
if one1 == two1 {
t.Errorf("AcquireRand() returned the same value twice!")
}

4
go.mod

@ -3,11 +3,11 @@ module git.tcp.direct/kayos/common
go 1.19
require (
golang.org/x/crypto v0.24.0
golang.org/x/crypto v0.14.0
nullprogram.com/x/rng v1.1.0
)
require golang.org/x/sys v0.21.0 // indirect
require golang.org/x/sys v0.13.0 // indirect
retract (
v0.9.1 // premature (race condition)

8
go.sum

@ -1,6 +1,6 @@
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
nullprogram.com/x/rng v1.1.0 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
nullprogram.com/x/rng v1.1.0/go.mod h1:glGw6V87vyfawxCzqOABL3WfL95G65az9Z2JZCylCkg=

@ -1,6 +1,7 @@
package hash
import (
"bytes"
"crypto/md5" //nolint:gosec
"crypto/sha1" //nolint:gosec
"crypto/sha256"
@ -8,8 +9,6 @@ import (
"errors"
"fmt"
"hash"
"hash/crc32"
"hash/crc64"
"io"
"os"
"sync"
@ -26,41 +25,8 @@ const (
TypeSHA256
TypeSHA512
TypeMD5
TypeCRC32
TypeCRC64ISO
TypeCRC64ECMA
)
var typeToString = map[Type]string{
TypeNull: "null", TypeBlake2b: "blake2b", TypeSHA1: "sha1",
TypeSHA256: "sha256", TypeSHA512: "sha512",
TypeMD5: "md5", TypeCRC32: "crc32",
TypeCRC64ISO: "crc64-iso", TypeCRC64ECMA: "crc64-ecma",
}
var stringToType = map[string]Type{
"null": TypeNull, "blake2b": TypeBlake2b, "sha1": TypeSHA1,
"sha256": TypeSHA256, "sha512": TypeSHA512,
"md5": TypeMD5, "crc32": TypeCRC32,
"crc64-iso": TypeCRC64ISO, "crc64-ecma": TypeCRC64ECMA,
}
func StringToType(s string) Type {
t, ok := stringToType[s]
if !ok {
return TypeNull
}
return t
}
func (t Type) String() string {
s, ok := typeToString[t]
if !ok {
return "unknown"
}
return s
}
var (
sha1Pool = &sync.Pool{
New: func() interface{} {
@ -88,24 +54,6 @@ var (
return h
},
}
crc32Pool = &sync.Pool{
New: func() interface{} {
return crc32.NewIEEE()
},
}
crc64ISOPool = &sync.Pool{
New: func() interface{} {
// ISO and ECMA are pre-computed in the stdlib, so Make is just fetching them, not computing them.
h := crc64.New(crc64.MakeTable(crc64.ISO))
return h
},
}
crc64ECMAPool = &sync.Pool{
New: func() interface{} {
h := crc64.New(crc64.MakeTable(crc64.ECMA))
return h
},
}
)
func Sum(ht Type, b []byte) []byte {
@ -126,16 +74,6 @@ func Sum(ht Type, b []byte) []byte {
case TypeMD5:
h = md5Pool.Get().(hash.Hash)
defer md5Pool.Put(h)
case TypeCRC32:
h = crc32Pool.Get().(hash.Hash)
defer crc32Pool.Put(h)
case TypeCRC64ISO:
h = crc64ISOPool.Get().(hash.Hash)
defer crc64ISOPool.Put(h)
case TypeCRC64ECMA:
h = crc64ECMAPool.Get().(hash.Hash)
defer crc64ECMAPool.Put(h)
default:
return nil
}
@ -145,8 +83,13 @@ func Sum(ht Type, b []byte) []byte {
return sum
}
// SumFile will attempt to calculate a blake2b checksum of the given file path's contents.
func SumFile(ht Type, path string) (buf []byte, err error) {
// Blake2bSum ignores all errors and gives you a blakae2b 64 hash value as a byte slice. (or panics somehow)
func Blake2bSum(b []byte) []byte {
return Sum(TypeBlake2b, b)
}
// BlakeFileChecksum will attempt to calculate a blake2b checksum of the given file path's contents.
func BlakeFileChecksum(path string) (buf []byte, err error) {
var f *os.File
f, err = os.Open(path)
if err != nil {
@ -164,5 +107,10 @@ func SumFile(ht Type, path string) (buf []byte, err error) {
return nil, errors.New("file is empty")
}
return Sum(ht, buf), nil
return Sum(TypeBlake2b, buf), 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 {
return bytes.Equal(Blake2bSum(a), Blake2bSum(b))
}

@ -4,136 +4,101 @@ import (
"bytes"
"encoding/base64"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"git.tcp.direct/kayos/common/entropy"
"git.tcp.direct/kayos/common/squish"
)
const (
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
kayosMD5 = "aoNjwileNitB208vOwpIow=="
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
kayosCRC32 = "xtig5w=="
kayosCRC64ISO = "YVx8IpQawAA="
kayosCRC64ECMA = "Nn5+vneo4j4="
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
kayosMD5 = "aoNjwileNitB208vOwpIow=="
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
)
var kayosByteSlice = []byte{107, 97, 121, 111, 115, 10}
var (
ogsha1, _ = base64.StdEncoding.DecodeString(kayosSHA1)
ogsha256, _ = base64.StdEncoding.DecodeString(kayosSHA256)
ogsha512, _ = base64.StdEncoding.DecodeString(kayosSHA512)
ogmd5, _ = base64.StdEncoding.DecodeString(kayosMD5)
ogBlake2b, _ = base64.StdEncoding.DecodeString(kayosBlake2b)
ogCRC32, _ = base64.StdEncoding.DecodeString(kayosCRC32)
ogCRC64ISO, _ = base64.StdEncoding.DecodeString(kayosCRC64ISO)
ogCRC64ECMA, _ = base64.StdEncoding.DecodeString(kayosCRC64ECMA)
valids = map[Type][]byte{
TypeSHA1: ogsha1,
TypeSHA256: ogsha256,
TypeSHA512: ogsha512,
TypeMD5: ogmd5,
TypeCRC32: ogCRC32,
TypeCRC64ISO: ogCRC64ISO,
TypeCRC64ECMA: ogCRC64ECMA,
TypeBlake2b: ogBlake2b,
func TestBlake2bsum(t *testing.T) {
og := squish.B64d(kayosBlake2b)
newc := Blake2bSum([]byte("kayos\n"))
if !bytes.Equal(newc, og) {
t.Fatalf("wanted: %v, got %v", kayosBlake2b, squish.B64e(newc))
}
)
if !BlakeEqual([]byte("kayos\n"), []byte{107, 97, 121, 111, 115, 10}) {
t.Fatalf("BlakeEqual should have been true. %s should == %s", []byte("kayos\n"), []byte{107, 97, 121, 111, 115, 92, 110})
}
t.Logf("[blake2bSum] success: %s", kayosBlake2b)
}
func TestBlakeFileChecksum(t *testing.T) {
path := t.TempDir() + "/blake2b.dat"
err := os.WriteFile(path, []byte{107, 97, 121, 111, 115, 10}, os.ModePerm)
if err != nil {
t.Errorf("[FAIL] failed to write test fle for TestBlakeFileChecksum: %s", err.Error())
}
filecheck, err2 := BlakeFileChecksum(path)
if err2 != nil {
t.Errorf("[FAIL] failed to read test fle for TestBlakeFileChecksum: %s", err2.Error())
}
if len(filecheck) == 0 {
t.Errorf("[FAIL] got nil output from BlakeFileChecksum")
}
if !bytes.Equal(filecheck, squish.B64d(kayosBlake2b)) {
t.Fatalf("[FAIL] wanted: %v, got %v", kayosBlake2b, squish.B64e(filecheck))
}
badfile, err3 := BlakeFileChecksum(t.TempDir() + "/" + entropy.RandStr(50))
if err3 == nil {
t.Errorf("[FAIL] shouldn't have been able to read phony file")
}
if len(badfile) != 0 {
t.Errorf("[FAIL] got non-nil output from bogus file: %v", badfile)
}
if !bytes.Equal(filecheck, squish.B64d(kayosBlake2b)) {
t.Fatalf("[FAIL] wanted: %v, got %v", kayosBlake2b, squish.B64e(filecheck))
}
err = os.WriteFile(path+".empty", []byte{}, os.ModePerm)
if err != nil {
t.Errorf("[FAIL] failed to write test fle for TestBlakeFileChecksum: %s", err.Error())
}
_, err4 := BlakeFileChecksum(path + ".empty")
if err4 == nil {
t.Fatalf("[FAIL] should have failed to read empty file")
}
}
func TestSum(t *testing.T) {
t.Parallel()
if Sum(TypeNull, []byte("yeet")) != nil {
t.Fatal("Sum(TypeNull, []byte(\"yeet\")) should have returned nil")
}
for k, v := range valids {
typeToTest := k
valueToTest := v
t.Run(typeToTest.String()+"/string_check", func(t *testing.T) {
t.Parallel()
if !strings.EqualFold(typeToTest.String(), StringToType(typeToTest.String()).String()) {
t.Errorf("[FAIL] %s: wanted %s, got %s",
typeToTest.String(), typeToTest.String(), StringToType(typeToTest.String()).String(),
)
}
})
t.Run(typeToTest.String()+"/static_check", func(t *testing.T) {
t.Parallel()
mySum := Sum(typeToTest, kayosByteSlice)
if !bytes.Equal(mySum, valueToTest) {
t.Errorf("[FAIL] %s: wanted %v, got %v", typeToTest.String(), valueToTest, mySum)
}
})
t.Run(typeToTest.String()+"/file_check", func(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), typeToTest.String()) // for coverage
if err := os.WriteFile(path, kayosByteSlice, os.ModePerm); err != nil {
t.Fatalf("[FAIL] failed to write test fle for TestSum: %s", err.Error())
}
res, err := SumFile(typeToTest, path)
if err != nil {
t.Fatalf("[FAIL] failed to read test fle for TestSum: %s", err.Error())
}
if !bytes.Equal(res, valueToTest) {
t.Errorf("[FAIL] %s: wanted %v, got %v", typeToTest.String(), valueToTest, res)
}
})
}
t.Run("bad file", func(t *testing.T) {
t.Parallel()
_, err := SumFile(TypeSHA1, "/dev/null")
if err == nil {
t.Fatal("SumFile should have returned an error")
}
if _, err = SumFile(TypeSHA1, entropy.RandStrWithUpper(500)); err == nil {
t.Fatal("SumFile should have returned an error")
}
})
t.Run("unknown type", func(t *testing.T) {
t.Parallel()
if Type(uint8(94)).String() != "unknown" {
t.Fatal("Type(uint(9453543)).String() should have returned \"unknown\"")
}
if StringToType(entropy.RandStr(10)) != TypeNull {
t.Fatal("bogus string should have returned TypeNull")
}
})
}
var (
ogsha1, _ = base64.StdEncoding.DecodeString(kayosSHA1)
ogsha256, _ = base64.StdEncoding.DecodeString(kayosSHA256)
ogsha512, _ = base64.StdEncoding.DecodeString(kayosSHA512)
ogmd5, _ = base64.StdEncoding.DecodeString(kayosMD5)
newsha1 = Sum(TypeSHA1, []byte("kayos\n"))
newsha256 = Sum(TypeSHA256, []byte("kayos\n"))
newsha512 = Sum(TypeSHA512, []byte("kayos\n"))
newmd5 = Sum(TypeMD5, []byte("kayos\n"))
)
func BenchmarkSum(b *testing.B) {
runIt := func(length int) {
dat := []byte(entropy.RandStrWithUpper(length))
b.Run(strconv.Itoa(length)+"char", func(b *testing.B) {
for sumType := range valids {
b.Run(sumType.String(), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.SetBytes(int64(len(dat)))
Sum(sumType, dat)
}
})
}
})
if !bytes.Equal(newsha1, ogsha1) {
t.Fatalf("[sha1] wanted: %v, got %v", ogsha1, newsha1)
}
for i := 0; i != 5; i++ {
mult := 5
if i == 0 {
runIt(50)
continue
}
if i > 1 {
mult = (mult * 10) * i
}
if i > 3 {
mult = (mult * 100) * i
}
runIt(i * mult)
t.Logf("[sha1] success: %s", kayosSHA1)
if !bytes.Equal(newsha256, ogsha256) {
t.Fatalf("[sha256] wanted: %v, got %v", ogsha256, newsha256)
}
t.Logf("[sha256] success: %s", kayosSHA256)
if !bytes.Equal(newsha512, ogsha512) {
t.Fatalf("[sha512] wanted: %v, got %v", ogsha512, newsha512)
}
t.Logf("[sha512] success: %s", kayosSHA512)
if !bytes.Equal(newmd5, ogmd5) {
t.Fatalf("[md5] wanted: %v, got %v", ogmd5, newmd5)
}
t.Logf("[md5] success: %s", kayosMD5)
}