hash: add support for BLAKE3, XXH3, XXH128

This commit is contained in:
albertony 2023-11-17 11:03:07 +01:00
parent a78bc093de
commit a6273a904d
4 changed files with 34 additions and 1 deletions

View file

@ -15,6 +15,8 @@ import (
"strings"
"github.com/jzelinskie/whirlpool"
"github.com/zeebo/blake3"
"github.com/zeebo/xxh3"
)
// Type indicates a standard hashing algorithm
@ -87,14 +89,36 @@ var (
// SHA256 indicates SHA-256 support
SHA256 Type
// BLAKE3 indicates BLAKE3 support
BLAKE3 Type
// XXH3 indicates XXH3 support, also known as XXH3-64, a variant of xxHash
XXH3 Type
// XXH128 indicates XXH128 support, also known as XXH3-128, a variant of xxHash
XXH128 Type
)
type xxh128Hasher struct {
xxh3.Hasher
}
// Sum overrides xxh3.Sum to return value based on Sum128 instead of the default Sum64.
func (h *xxh128Hasher) Sum(b []byte) []byte {
buf := h.Sum128().Bytes()
return buf[:]
}
func init() {
MD5 = RegisterHash("md5", "MD5", 32, md5.New)
SHA1 = RegisterHash("sha1", "SHA-1", 40, sha1.New)
Whirlpool = RegisterHash("whirlpool", "Whirlpool", 128, whirlpool.New)
CRC32 = RegisterHash("crc32", "CRC-32", 8, func() hash.Hash { return crc32.NewIEEE() })
SHA256 = RegisterHash("sha256", "SHA-256", 64, sha256.New)
BLAKE3 = RegisterHash("blake3", "BLAKE3", 64, func() hash.Hash { return blake3.New() })
XXH3 = RegisterHash("xxh3", "XXH3", 16, func() hash.Hash { return xxh3.New() })
XXH128 = RegisterHash("xxh128", "XXH128", 32, func() hash.Hash { return &xxh128Hasher{} })
}
// Supported returns a set of all the supported hashes by

View file

@ -77,6 +77,9 @@ var hashTestSet = []hashTest{
hash.Whirlpool: "eddf52133d4566d763f716e853d6e4efbabd29e2c2e63f56747b1596172851d34c2df9944beb6640dbdbe3d9b4eb61180720a79e3d15baff31c91e43d63869a4",
hash.CRC32: "a6041d7e",
hash.SHA256: "c839e57675862af5c21bd0a15413c3ec579e0d5522dab600bc6c3489b05b8f54",
hash.BLAKE3: "0a7276a407a3be1b4d31488318ee05a335aad5a3b82c4420e592a8178c9e86bb",
hash.XXH3: "4b83b0c51c543525",
hash.XXH128: "438de241a57d684214f67657f7aad93b",
},
},
// Empty data set
@ -88,6 +91,9 @@ var hashTestSet = []hashTest{
hash.Whirlpool: "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3",
hash.CRC32: "00000000",
hash.SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
hash.BLAKE3: "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
hash.XXH3: "2d06800538d394c2",
hash.XXH128: "99aa06d3014798d86001c324468d497f",
},
},
}

3
go.mod
View file

@ -76,6 +76,8 @@ require (
github.com/xanzy/ssh-agent v0.3.3
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78
github.com/yunify/qingstor-sdk-go/v3 v3.2.0
github.com/zeebo/blake3 v0.2.3
github.com/zeebo/xxh3 v1.0.2
go.etcd.io/bbolt v1.3.10
goftp.io/server/v2 v2.0.1
golang.org/x/crypto v0.29.0
@ -197,7 +199,6 @@ require (
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/willscott/go-nfs-client v0.0.0-20240104095149-b44639837b00 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
github.com/zeebo/errs v1.3.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect

2
go.sum
View file

@ -643,6 +643,8 @@ github.com/zeebo/errs v1.3.0 h1:hmiaKqgYZzcVgRL1Vkc1Mn2914BbzB0IBxs+ebeutGs=
github.com/zeebo/errs v1.3.0/go.mod h1:sgbWHsvVuTPHcqJJGQ1WhI5KbWlHYz+2+2C/LSEtCw4=
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0=
go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=