Feat: Add new hash functions + Testing: improve coverage

This commit is contained in:
kayos@tcp.direct 2022-07-04 00:43:03 -07:00
parent 06cc556626
commit 3d8d392d8b
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
8 changed files with 143 additions and 65 deletions

View File

@ -14,7 +14,7 @@ func RandomStrChoice(choice []string) string {
if len(choice) > 0 {
return choice[RNGUint32()%uint32(len(choice))]
}
return choice[0]
return ""
}
// GetCryptoSeed returns a random int64 derived from crypto/rand.

View File

@ -43,8 +43,6 @@ func Test_RandStr(t *testing.T) {
one := RandStr(55)
t.Logf("Random0: %s Random1: %s", zero, one)
randStrChecks(zero, one, t)
zero = ""
one = ""
}
}
@ -70,14 +68,15 @@ func Test_RandStr_Entropy(t *testing.T) {
zero, one, similarity)
}
t.Logf("[ENTROPY] Similarity score (lower is better): %d", similarity)
totalScore = totalScore + similarity
zero = ""
one = ""
totalScore += similarity
}
t.Logf("[ENTROPY] final score (lower is better): %d", totalScore)
}
func Test_RandomStrChoice(t *testing.T) {
if RandomStrChoice([]string{}) != "" {
t.Fatalf("RandomStrChoice returned a value when given an empty slice")
}
var slice []string
for n := 0; n != 500; n++ {
slice = append(slice, RandStr(555))

1
go.mod
View File

@ -3,6 +3,7 @@ module git.tcp.direct/kayos/common
go 1.18
require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.26.1
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6

1
go.sum
View File

@ -1,6 +1,7 @@
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=

View File

@ -1,48 +1,80 @@
package hash
import (
"golang.org/x/crypto/blake2b"
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"io"
"os"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
)
type Type int8
const (
TypeNull Type = iota
TypeBlake2b
TypeSHA1
TypeSHA256
TypeSHA512
TypeMD5
)
func Sum(ht Type, b []byte) []byte {
var h hash.Hash
switch ht {
case TypeBlake2b:
h, _ := blake2b.New(blake2b.Size, nil)
h.Write(b)
return h.Sum(nil)
case TypeSHA1:
h = sha1.New()
case TypeSHA256:
h = sha256.New()
case TypeSHA512:
h = sha512.New()
case TypeMD5:
h = md5.New()
default:
return nil
}
h.Write(b)
return h.Sum(nil)
}
// 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)
return Sum(TypeBlake2b, b)
}
// BlakeFileChecksum takes in the path of a file on the OS' filesystem and will attempt to calculate a blake2b checksum of the file's contents.
func BlakeFileChecksum(filename string) ([]byte, error) {
f, err := os.Open(filename)
defer f.Close()
empty := []byte{}
// 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 {
return empty, err
return nil, err
}
buf, err := io.ReadAll(f)
defer func() {
if closeErr := f.Close(); err != nil {
err = errors.Wrapf(err, "failed to close file: %s", closeErr)
}
}()
if err != nil {
return empty, err
buf, _ = io.ReadAll(f)
if len(buf) == 0 {
return nil, errors.New("file is empty")
}
hasha, err := blake2b.New(64, nil)
if err != nil {
return empty, err
}
hasha.Write(buf)
return hasha.Sum(nil), 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 {
ahash := Blake2bSum(a)
bhash := Blake2bSum(b)
return string(ahash) == string(bhash)
return bytes.Equal(Blake2bSum(a), Blake2bSum(b))
}

View File

@ -9,43 +9,95 @@ import (
"git.tcp.direct/kayos/common/squish"
)
const kayos = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
const (
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
kayosMD5 = "aoNjwileNitB208vOwpIow=="
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
)
func TestBlake2bsum(t *testing.T) {
og := squish.B64d(kayos)
og := squish.B64d(kayosBlake2b)
newc := Blake2bSum([]byte("kayos\n"))
if !bytes.Equal(newc, og) {
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(newc))
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("failed to write test fle for TestBlakeFileChecksum: %s", err.Error())
t.Errorf("[FAIL] failed to write test fle for TestBlakeFileChecksum: %s", err.Error())
}
filecheck, err2 := BlakeFileChecksum(path)
if err2 != nil {
t.Errorf("failed to read test fle for TestBlakeFileChecksum: %s", err2.Error())
t.Errorf("[FAIL] failed to read test fle for TestBlakeFileChecksum: %s", err2.Error())
}
if len(filecheck) == 0 {
t.Errorf("Got nil output from BlakeFileChecksum")
t.Errorf("[FAIL] got nil output from BlakeFileChecksum")
}
if !bytes.Equal(filecheck, squish.B64d(kayos)) {
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(filecheck))
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("shouldn't have been able to read phony file")
t.Errorf("[FAIL] shouldn't have been able to read phony file")
}
if len(badfile) != 0 {
t.Errorf("Got non-nil output from bogus file: %v", badfile)
t.Errorf("[FAIL] got non-nil output from bogus file: %v", badfile)
}
if !bytes.Equal(filecheck, squish.B64d(kayos)) {
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(filecheck))
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) {
if Sum(TypeNull, []byte("yeet")) != nil {
t.Fatal("Sum(TypeNull, []byte(\"yeet\")) should have returned nil")
}
var (
ogsha1 = squish.B64d(kayosSHA1)
ogsha256 = squish.B64d(kayosSHA256)
ogsha512 = squish.B64d(kayosSHA512)
ogmd5 = squish.B64d(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"))
)
if !bytes.Equal(newsha1, ogsha1) {
t.Fatalf("[sha1] wanted: %v, got %v", kayosSHA1, squish.B64e(newsha1))
}
t.Logf("[sha1] success: %s", kayosSHA1)
if !bytes.Equal(newsha256, ogsha256) {
t.Fatalf("[sha256] wanted: %v, got %v", kayosSHA256, squish.B64e(newsha256))
}
t.Logf("[sha256] success: %s", kayosSHA256)
if !bytes.Equal(newsha512, ogsha512) {
t.Fatalf("[sha512] wanted: %v, got %v", kayosSHA512, squish.B64e(newsha512))
}
t.Logf("[sha512] success: %s", kayosSHA512)
if !bytes.Equal(newmd5, ogmd5) {
t.Fatalf("[md5] wanted: %v, got %v", kayosMD5, squish.B64e(newmd5))
}
t.Logf("[md5] success: %s", kayosMD5)
}

View File

@ -49,10 +49,7 @@ const (
// GetUname uses system calls to retrieve the same values as the uname linux command
func GetUname(unameFlags string) (un string, err error) {
ub := &syscall.Utsname{}
err = syscall.Uname(ub)
if err != nil {
return
}
_ = syscall.Uname(ub)
var targets []*[65]int8
for _, n := range unameFlags {
var flag = [1]string{string(n)}

View File

@ -19,42 +19,33 @@ Mauris ut mi quis est vehicula molestie. Mauris eu varius urna. Integer sodales
func TestGzip(t *testing.T) {
gsUp := Gzip([]byte(lip))
if bytes.Equal(gsUp, []byte(lip)) {
t.Fatalf("Gzip didn't change the data at all despite being error free...")
t.Fatalf("[FAIL] Gzip didn't change the data at all despite being error free...")
}
if len(gsUp) == len([]byte(lip)) || len(gsUp) > len([]byte(lip)) {
t.Fatalf("Gzip didn't change the sise of the data at all (or it grew)...")
t.Fatalf("[FAIL] Gzip didn't change the sise of the data at all (or it grew)...")
}
if len(gsUp) == 0 {
t.Fatalf("[FAIL] ended up with 0 bytes after compression...")
}
profit := len([]byte(lip)) - len(gsUp)
t.Logf("[PASS] Gzip compress succeeded, squished %d bytes.", profit)
hosDown, err := Gunzip(gsUp)
if err != nil {
t.Fatalf("Gzip decompression failed: %e", err)
}
if !bytes.Equal(hosDown, []byte(lip)) {
t.Fatalf("Gzip decompression failed, data does not appear to be the same after decompression")
t.Fatalf("[FAIL] Gzip decompression failed, data does not appear to be the same after decompression")
}
if len(hosDown) != len([]byte(lip)) {
t.Fatalf("Gzip decompression failed, data [%d] does not appear to be the same [%d] length after decompression", hosDown, len([]byte(lip)))
t.Fatalf("[FAIL] Gzip decompression failed, data [%d] does not appear to be the same [%d] length after decompression", hosDown, len([]byte(lip)))
}
t.Logf("[PASS] Gzip decompress succeeded, restored %d bytes.", profit)
_, err = Gunzip(nil)
if err == nil {
t.Fatalf("[FAIL] Gunzip didn't fail on nil input")
}
}
func TestUnpackStr(t *testing.T) {
packed := B64e(Gzip([]byte(lip)))
unpacked, err := UnpackStr(packed)
@ -62,8 +53,13 @@ func TestUnpackStr(t *testing.T) {
case err != nil:
t.Fatalf("[FAIL] %e", err)
case unpacked != lip:
t.Fatalf("unpackstr decided to not work, who knows why. If you see this than I have already become a janitor.")
t.Fatalf("[FAIL] unpackstr decided to not work, who knows why. If you see this than I have already become a janitor.")
default:
t.Logf("[PASS] TestUnpackStr")
}
_, nilerr := UnpackStr("")
if nilerr == nil {
t.Fatalf("[FAIL] unpackstr didn't fail on empty input")
}
}