Compare commits

..

2 Commits

9 changed files with 196 additions and 208 deletions

@ -42,7 +42,7 @@ jobs:
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL - name: Initialize CodeQL
uses: github/codeql-action/init@v3 uses: github/codeql-action/init@v2
with: with:
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file. # 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). # 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) # If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild - name: Autobuild
uses: github/codeql-action/autobuild@v3 uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell. # Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl # 📚 https://git.io/JvXDl
@ -67,4 +67,4 @@ jobs:
# make release # make release
- name: Perform CodeQL Analysis - 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 - uses: actions/checkout@v4
with: with:
fetch-depth: 2 fetch-depth: 2
- uses: actions/setup-go@v5 - uses: actions/setup-go@v4
with: with:
go-version: '1.18' go-version: '1.18'
- name: Run coverage - name: Run coverage

@ -2,12 +2,67 @@ package common
import ( import (
"errors" "errors"
"fmt"
"io"
"os"
"sync" "sync"
"testing" "testing"
"git.tcp.direct/kayos/common/entropy" "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) { func TestAbs(t *testing.T) {
var start = int32(entropy.RNG(5)) var start = int32(entropy.RNG(5))
for start < 1 { for start < 1 {

@ -4,8 +4,11 @@ import (
crip "crypto/rand" crip "crypto/rand"
"encoding/binary" "encoding/binary"
"math/rand" "math/rand"
"os"
"reflect"
"sync" "sync"
"time" "time"
"unsafe"
"nullprogram.com/x/rng" "nullprogram.com/x/rng"
@ -16,11 +19,11 @@ type randPool struct {
sync.Pool sync.Pool
} }
func (p *randPool) Get() *rand.Rand { func (p *randPool) Get() *rng.SplitMix64 {
return p.Pool.Get().(*rand.Rand) return p.Pool.Get().(*rng.SplitMix64)
} }
func (p *randPool) Put(r *rand.Rand) { func (p *randPool) Put(r *rng.SplitMix64) {
p.Pool.Put(r) p.Pool.Put(r)
} }
@ -30,8 +33,7 @@ var (
New: func() interface{} { New: func() interface{} {
sm64 := new(rng.SplitMix64) sm64 := new(rng.SplitMix64)
sm64.Seed(GetCryptoSeed()) sm64.Seed(GetCryptoSeed())
prng := rand.New(sm64) //nolint:gosec return sm64
return prng
}, },
}, },
} }
@ -42,16 +44,18 @@ var (
func setSharedRand() { func setSharedRand() {
hardLocc.Lock() hardLocc.Lock()
sharedRand = lolXD.Get() sharedRand = rand.New(lolXD.Get())
hardLocc.Unlock() hardLocc.Unlock()
} }
func AcquireRand() *rand.Rand { func AcquireRand() *rand.Rand {
return lolXD.Get() return rand.New(lolXD.Get())
} }
func ReleaseRand(r *rand.Rand) { 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 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. // 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 { func GetSharedRand() *rand.Rand {
getSharedRand.Do(func() { getSharedRand.Do(func() {
setSharedRand() setSharedRand()
}) })
return sharedRand return rand.New(sharedRand)
} }
// RNGUint32 returns a random uint32 using crypto/rand and splitmix64. // RNGUint32 returns a random uint32 using crypto/rand and splitmix64.
func RNGUint32() uint32 { func RNGUint32() uint32 {
r := lolXD.Get() r := lolXD.Get()
ui := r.Uint32() ui := uint32(r.Int63() >> 31)
lolXD.Put(r) lolXD.Put(r)
return ui 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 { func RNG(n int) int {
r := lolXD.Get() r := lolXD.Get()
i := r.Intn(n) defer lolXD.Put(r)
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 return i
} }
@ -167,7 +187,7 @@ func randStr(upper bool, size int) string {
buf := strBufs.Get() buf := strBufs.Get()
r := lolXD.Get() r := lolXD.Get()
for i := 0; i != size; i++ { for i := 0; i != size; i++ {
ui32 := int(r.Uint32()) ui32 := int(r.Int63() >> 31)
switch upper { switch upper {
case true: case true:
_ = buf.WriteByte(charsetWithUpper[ui32%len(charsetWithUpper)]) _ = 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!") t.Errorf("GetSharedRand(55555) returned the same value twice!")
} }
r := AcquireRand() r := AcquireRand()
one := r.Intn(55555) one := r.Uint64()
two := r.Intn(55555) two := r.Uint64()
if one == two { if one == two {
t.Errorf("AcquireRand() returned the same value twice!") t.Errorf("AcquireRand() returned the same value twice!")
} }
ReleaseRand(r) ReleaseRand(r)
r = AcquireRand() r = AcquireRand()
one1 := r.Intn(55555) one1 := r.Uint64()
two1 := r.Intn(55555) two1 := r.Uint64()
if one1 == two1 { if one1 == two1 {
t.Errorf("AcquireRand() returned the same value twice!") t.Errorf("AcquireRand() returned the same value twice!")
} }

4
go.mod

@ -3,11 +3,11 @@ module git.tcp.direct/kayos/common
go 1.19 go 1.19
require ( require (
golang.org/x/crypto v0.24.0 golang.org/x/crypto v0.14.0
nullprogram.com/x/rng v1.1.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 ( retract (
v0.9.1 // premature (race condition) 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.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
nullprogram.com/x/rng v1.1.0/go.mod h1:glGw6V87vyfawxCzqOABL3WfL95G65az9Z2JZCylCkg= nullprogram.com/x/rng v1.1.0/go.mod h1:glGw6V87vyfawxCzqOABL3WfL95G65az9Z2JZCylCkg=

@ -1,6 +1,7 @@
package hash package hash
import ( import (
"bytes"
"crypto/md5" //nolint:gosec "crypto/md5" //nolint:gosec
"crypto/sha1" //nolint:gosec "crypto/sha1" //nolint:gosec
"crypto/sha256" "crypto/sha256"
@ -8,8 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"hash" "hash"
"hash/crc32"
"hash/crc64"
"io" "io"
"os" "os"
"sync" "sync"
@ -26,41 +25,8 @@ const (
TypeSHA256 TypeSHA256
TypeSHA512 TypeSHA512
TypeMD5 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 ( var (
sha1Pool = &sync.Pool{ sha1Pool = &sync.Pool{
New: func() interface{} { New: func() interface{} {
@ -88,24 +54,6 @@ var (
return h 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 { func Sum(ht Type, b []byte) []byte {
@ -126,16 +74,6 @@ func Sum(ht Type, b []byte) []byte {
case TypeMD5: case TypeMD5:
h = md5Pool.Get().(hash.Hash) h = md5Pool.Get().(hash.Hash)
defer md5Pool.Put(h) 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: default:
return nil return nil
} }
@ -145,8 +83,13 @@ func Sum(ht Type, b []byte) []byte {
return sum return sum
} }
// SumFile will attempt to calculate a blake2b checksum of the given file path's contents. // Blake2bSum ignores all errors and gives you a blakae2b 64 hash value as a byte slice. (or panics somehow)
func SumFile(ht Type, path string) (buf []byte, err error) { 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 var f *os.File
f, err = os.Open(path) f, err = os.Open(path)
if err != nil { 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 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" "bytes"
"encoding/base64" "encoding/base64"
"os" "os"
"path/filepath"
"strconv"
"strings"
"testing" "testing"
"git.tcp.direct/kayos/common/entropy" "git.tcp.direct/kayos/common/entropy"
"git.tcp.direct/kayos/common/squish"
) )
const ( const (
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag==" kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
kayosMD5 = "aoNjwileNitB208vOwpIow==" kayosMD5 = "aoNjwileNitB208vOwpIow=="
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss=" kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U=" kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA==" kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
kayosCRC32 = "xtig5w=="
kayosCRC64ISO = "YVx8IpQawAA="
kayosCRC64ECMA = "Nn5+vneo4j4="
) )
var kayosByteSlice = []byte{107, 97, 121, 111, 115, 10} func TestBlake2bsum(t *testing.T) {
og := squish.B64d(kayosBlake2b)
var ( newc := Blake2bSum([]byte("kayos\n"))
ogsha1, _ = base64.StdEncoding.DecodeString(kayosSHA1) if !bytes.Equal(newc, og) {
ogsha256, _ = base64.StdEncoding.DecodeString(kayosSHA256) t.Fatalf("wanted: %v, got %v", kayosBlake2b, squish.B64e(newc))
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,
} }
) 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) { func TestSum(t *testing.T) {
t.Parallel()
if Sum(TypeNull, []byte("yeet")) != nil { if Sum(TypeNull, []byte("yeet")) != nil {
t.Fatal("Sum(TypeNull, []byte(\"yeet\")) should have returned nil") t.Fatal("Sum(TypeNull, []byte(\"yeet\")) should have returned nil")
} }
for k, v := range valids { var (
typeToTest := k ogsha1, _ = base64.StdEncoding.DecodeString(kayosSHA1)
valueToTest := v ogsha256, _ = base64.StdEncoding.DecodeString(kayosSHA256)
t.Run(typeToTest.String()+"/string_check", func(t *testing.T) { ogsha512, _ = base64.StdEncoding.DecodeString(kayosSHA512)
t.Parallel() ogmd5, _ = base64.StdEncoding.DecodeString(kayosMD5)
if !strings.EqualFold(typeToTest.String(), StringToType(typeToTest.String()).String()) { newsha1 = Sum(TypeSHA1, []byte("kayos\n"))
t.Errorf("[FAIL] %s: wanted %s, got %s", newsha256 = Sum(TypeSHA256, []byte("kayos\n"))
typeToTest.String(), typeToTest.String(), StringToType(typeToTest.String()).String(), newsha512 = Sum(TypeSHA512, []byte("kayos\n"))
) newmd5 = Sum(TypeMD5, []byte("kayos\n"))
} )
})
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")
}
})
}
func BenchmarkSum(b *testing.B) { if !bytes.Equal(newsha1, ogsha1) {
runIt := func(length int) { t.Fatalf("[sha1] wanted: %v, got %v", ogsha1, newsha1)
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)
}
})
}
})
} }
for i := 0; i != 5; i++ { t.Logf("[sha1] success: %s", kayosSHA1)
mult := 5
if i == 0 { if !bytes.Equal(newsha256, ogsha256) {
runIt(50) t.Fatalf("[sha256] wanted: %v, got %v", ogsha256, newsha256)
continue
}
if i > 1 {
mult = (mult * 10) * i
}
if i > 3 {
mult = (mult * 100) * i
}
runIt(i * mult)
} }
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)
} }