mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
hash
This commit is contained in:
73
hash/hash.go
Normal file
73
hash/hash.go
Normal file
@ -0,0 +1,73 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"hash"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
"github.com/spaolacci/murmur3"
|
||||
"github.com/tjfoc/gmsm/sm3"
|
||||
)
|
||||
|
||||
func Sha1(msg []byte) []byte { return sum(sha1.New, msg) }
|
||||
|
||||
func Sha1Hex(msg []byte) string { return hex.EncodeToString(Sha1(msg)) }
|
||||
|
||||
func Sha1Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha1(msg)) }
|
||||
|
||||
func Sha224(msg []byte) []byte { return sum(sha256.New224, msg) }
|
||||
|
||||
func Sha224Hex(msg []byte) string { return hex.EncodeToString(Sha224(msg)) }
|
||||
|
||||
func Sha224Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha224(msg)) }
|
||||
|
||||
func Sha256(msg []byte) []byte { return sum(sha256.New, msg) }
|
||||
|
||||
func Sha256Hex(msg []byte) string { return hex.EncodeToString(Sha256(msg)) }
|
||||
|
||||
func Sha256Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha256(msg)) }
|
||||
|
||||
func Sha384(msg []byte) []byte { return sum(sha512.New384, msg) }
|
||||
|
||||
func Sha384Hex(msg []byte) string { return hex.EncodeToString(Sha384(msg)) }
|
||||
|
||||
func Sha384Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha384(msg)) }
|
||||
|
||||
func Sha512(msg []byte) []byte { return sum(sha512.New, msg) }
|
||||
|
||||
func Sha512Hex(msg []byte) string { return hex.EncodeToString(Sha512(msg)) }
|
||||
|
||||
func Sha512Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha512(msg)) }
|
||||
|
||||
func Sm3(msg []byte) []byte { return sum(sm3.New, msg) }
|
||||
|
||||
func Sm3Hex(msg []byte) string { return hex.EncodeToString(Sm3(msg)) }
|
||||
|
||||
func Sm3Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sm3(msg)) }
|
||||
|
||||
func Murmur3(msg []byte) uint64 {
|
||||
return murmur3.Sum64(msg)
|
||||
}
|
||||
|
||||
func XXhash(msg []byte) []byte {
|
||||
d := xxhash.New()
|
||||
_, _ = d.Write(msg)
|
||||
return d.Sum(nil)
|
||||
}
|
||||
|
||||
func XXHashUint64(msg []byte) uint64 {
|
||||
d := xxhash.New()
|
||||
_, _ = d.Write(msg)
|
||||
return d.Sum64()
|
||||
}
|
||||
|
||||
func sum(f func() hash.Hash, msg []byte) []byte {
|
||||
h := f()
|
||||
|
||||
_, _ = h.Write(msg)
|
||||
return h.Sum(nil)
|
||||
}
|
28
hash/hash_test.go
Normal file
28
hash/hash_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package hash_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/charlienet/go-mixed/hash"
|
||||
)
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
|
||||
t.Log(base64.StdEncoding.EncodeToString(hash.Sha1([]byte{0x31})))
|
||||
t.Log(hex.EncodeToString(hash.Sha1([]byte{0x31})))
|
||||
|
||||
}
|
||||
|
||||
func TestXXHash(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
t.Log(hex.EncodeToString(hash.XXhash([]byte(strconv.Itoa(i)))), " ", hash.XXHashUint64([]byte(strconv.Itoa(i))))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMurmur3(t *testing.T) {
|
||||
t.Log(hash.Murmur3([]byte("123")))
|
||||
t.Log(hash.XXHashUint64([]byte("123")))
|
||||
}
|
Reference in New Issue
Block a user