1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

优化哈希函数

This commit is contained in:
2022-03-29 10:02:34 +08:00
parent 421e8e191b
commit 42f85a2518
3 changed files with 111 additions and 65 deletions

View File

@ -1,6 +1,7 @@
package hash
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
@ -14,41 +15,19 @@ import (
"github.com/tjfoc/gmsm/sm3"
)
func Sha1(msg []byte) []byte { return sum(sha1.New, msg) }
func Md5(msg []byte) hashResult { return sum(md5.New, msg) }
func Sha1Hex(msg []byte) string { return hex.EncodeToString(Sha1(msg)) }
func Sha1(msg []byte) hashResult { return sum(sha1.New, msg) }
func Sha1Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha1(msg)) }
func Sha224(msg []byte) hashResult { return sum(sha256.New224, msg) }
func Sha224(msg []byte) []byte { return sum(sha256.New224, msg) }
func Sha256(msg []byte) hashResult { return sum(sha256.New, msg) }
func Sha224Hex(msg []byte) string { return hex.EncodeToString(Sha224(msg)) }
func Sha384(msg []byte) hashResult { return sum(sha512.New384, msg) }
func Sha224Base64(msg []byte) string { return base64.StdEncoding.EncodeToString(Sha224(msg)) }
func Sha512(msg []byte) hashResult { return sum(sha512.New, 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 Sm3(msg []byte) hashResult { return sum(sm3.New, msg) }
func Murmur3(msg []byte) uint64 {
return murmur3.Sum64(msg)
@ -78,9 +57,27 @@ func Funv64(msg []byte) uint64 {
return h.Sum64()
}
func sum(f func() hash.Hash, msg []byte) []byte {
type hashResult []byte
func sum(f func() hash.Hash, msg []byte) hashResult {
h := f()
_, _ = h.Write(msg)
return h.Sum(nil)
}
func (r hashResult) Hex() string {
return hex.EncodeToString(r)
}
func (r hashResult) Base64() string {
return base64.StdEncoding.EncodeToString(r)
}
func (r hashResult) Bytes() []byte {
return r
}
func (r hashResult) String() string {
return r.Hex()
}

View File

@ -1,8 +1,8 @@
package hash_test
import (
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
"testing"
@ -10,10 +10,8 @@ import (
)
func TestEncode(t *testing.T) {
t.Log(base64.StdEncoding.EncodeToString(hash.Sha1([]byte{0x31})))
t.Log(hex.EncodeToString(hash.Sha1([]byte{0x31})))
t.Log(hash.Sha1([]byte{0x31}).Base64())
t.Log(hash.Sha1([]byte{0x31}).Hex())
}
func TestXXHash(t *testing.T) {
@ -26,3 +24,56 @@ func TestMurmur3(t *testing.T) {
t.Log(hash.Murmur3([]byte("123")))
t.Log(hash.XXHashUint64([]byte("123")))
}
func TestFnv(t *testing.T) {
for i := 0; i < 100; i++ {
bytes := []byte(fmt.Sprintf("%d", i))
t.Log(hash.Funv32(bytes))
}
}
func BenchmarkHash(b *testing.B) {
bytes := []byte("abcdefdg")
b.Run("xxhash", func(b *testing.B) {
doBenchmark(func() {
hash.XXHashUint64(bytes)
}, b)
})
b.Run("murmur3", func(b *testing.B) {
doBenchmark(func() {
hash.Murmur3(bytes)
}, b)
})
b.Run("fnv", func(b *testing.B) {
doBenchmark(func() {
hash.Funv64(bytes)
}, b)
})
b.Run("sm3", func(b *testing.B) {
doBenchmark(func() {
hash.Sm3(bytes)
}, b)
})
b.Run("md5", func(b *testing.B) {
doBenchmark(func() {
hash.Md5(bytes)
}, b)
})
b.Run("sha256", func(b *testing.B) {
doBenchmark(func() {
hash.Sha256(bytes)
}, b)
})
}
func doBenchmark(f func(), b *testing.B) {
for i := 0; i < b.N; i++ {
f()
}
}

View File

@ -2,54 +2,52 @@ package hmac
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"hash"
"github.com/tjfoc/gmsm/sm3"
)
func Sha1(key, msg []byte) []byte { return sum(sha1.New, key, msg) }
func Md5(key, msg []byte) hashResult { return sum(md5.New, key, msg) }
func Sha1Hex(key, msg []byte) string { return hex.EncodeToString(Sha1(key, msg)) }
func Sha1(key, msg []byte) hashResult { return sum(sha1.New, key, msg) }
func Sha1Base64(key, msg []byte) string { return hex.EncodeToString(Sha1(key, msg)) }
func Sha224(key, msg []byte) hashResult { return sum(sha256.New224, key, msg) }
func Sha224(key, msg []byte) []byte { return sum(sha256.New224, key, msg) }
func Sha256(key, msg []byte) hashResult { return sum(sha256.New, key, msg) }
func Sha224Hex(key, msg []byte) string { return hex.EncodeToString(Sha224(key, msg)) }
func Sha384(key, msg []byte) hashResult { return sum(sha512.New384, key, msg) }
func Sha224Base64(key, msg []byte) string { return hex.EncodeToString(Sha224(key, msg)) }
func Sha512(key, msg []byte) hashResult { return sum(sha512.New, key, msg) }
func Sha256(key, msg []byte) []byte { return sum(sha256.New, key, msg) }
func Sm3(key, msg []byte) hashResult { return sum(sm3.New, key, msg) }
func Sha256Hex(key, msg []byte) string { return hex.EncodeToString(Sha256(key, msg)) }
func Sha256Base64(key, msg []byte) string { return hex.EncodeToString(Sha256(key, msg)) }
func Sha384(key, msg []byte) []byte { return sum(sha512.New384, key, msg) }
func Sha384Hex(key, msg []byte) string { return hex.EncodeToString(Sha384(key, msg)) }
func Sha384Base64(key, msg []byte) string { return hex.EncodeToString(Sha384(key, msg)) }
func Sha512(key, msg []byte) []byte { return sum(sha512.New, key, msg) }
func Sha512Hex(key, msg []byte) string { return hex.EncodeToString(Sha512(key, msg)) }
func Sha512Base64(key, msg []byte) string { return hex.EncodeToString(Sha512(key, msg)) }
func Sm3(key, msg []byte) []byte { return sum(sm3.New, key, msg) }
func Sm3Hex(key, msg []byte) string { return hex.EncodeToString(Sm3(key, msg)) }
func Sm3Base64(key, msg []byte) string { return hex.EncodeToString(Sm3(key, msg)) }
func sum(f func() hash.Hash, msg, key []byte) []byte {
func sum(f func() hash.Hash, msg, key []byte) hashResult {
h := hmac.New(f, key)
h.Write(msg)
return h.Sum(nil)
}
type hashResult []byte
func (r hashResult) Hex() string {
return hex.EncodeToString(r)
}
func (r hashResult) Base64() string {
return base64.StdEncoding.EncodeToString(r)
}
func (r hashResult) Bytes() []byte {
return r
}
func (r hashResult) String() string {
return r.Hex()
}