Feat[hash]: Add crc64 with 2 polynomial options
This commit is contained in:
parent
44e5d2d424
commit
5f109b6caf
25
hash/hash.go
25
hash/hash.go
|
@ -9,6 +9,7 @@ import (
|
|||
"fmt"
|
||||
"hash"
|
||||
"hash/crc32"
|
||||
"hash/crc64"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
@ -26,18 +27,22 @@ const (
|
|||
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 {
|
||||
|
@ -88,6 +93,19 @@ var (
|
|||
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 {
|
||||
|
@ -111,6 +129,13 @@ func Sum(ht Type, b []byte) []byte {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/base64"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -12,30 +13,36 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
|
||||
kayosMD5 = "aoNjwileNitB208vOwpIow=="
|
||||
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
|
||||
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
|
||||
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
|
||||
kayosCRC32 = "xtig5w=="
|
||||
kayosBlake2b = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
|
||||
kayosMD5 = "aoNjwileNitB208vOwpIow=="
|
||||
kayosSHA1 = "M23ElC0sQYAK+MaMZVmza2L8mss="
|
||||
kayosSHA256 = "BagY0TmoGR3O7t80BGm4K6UHPlqEg6HJirwQmhrPK4U="
|
||||
kayosSHA512 = "xiuo2na76acrWXCTTR++O1pPZabOhyj8nbfb5Go3e1pEq9VJYIsOioTXalf2GCuERmFecWkmaL5QI8mIXXWpNA=="
|
||||
kayosCRC32 = "xtig5w=="
|
||||
kayosCRC64ISO = "YVx8IpQawAA="
|
||||
kayosCRC64ECMA = "Nn5+vneo4j4="
|
||||
)
|
||||
|
||||
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)
|
||||
valids = map[Type][]byte{
|
||||
TypeSHA1: ogsha1,
|
||||
TypeSHA256: ogsha256,
|
||||
TypeSHA512: ogsha512,
|
||||
TypeMD5: ogmd5,
|
||||
TypeCRC32: ogCRC32,
|
||||
TypeBlake2b: ogBlake2b,
|
||||
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,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -51,7 +58,9 @@ func TestSum(t *testing.T) {
|
|||
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.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) {
|
||||
|
@ -97,17 +106,34 @@ func TestSum(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
var benchData = []byte(entropy.RandStrWithUpper(5000))
|
||||
|
||||
func BenchmarkSum(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(benchData)))
|
||||
Sum(sumType, benchData)
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue