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

bytesconv

This commit is contained in:
2022-03-29 10:24:43 +08:00
parent 42f85a2518
commit bd4a7aed01
6 changed files with 86 additions and 91 deletions

View File

@ -5,29 +5,28 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"hash"
"hash/fnv"
"github.com/cespare/xxhash/v2"
"github.com/charlienet/go-mixed/bytesconv"
"github.com/spaolacci/murmur3"
"github.com/tjfoc/gmsm/sm3"
)
func Md5(msg []byte) hashResult { return sum(md5.New, msg) }
func Md5(msg []byte) bytesconv.BytesResult { return sum(md5.New, msg) }
func Sha1(msg []byte) hashResult { return sum(sha1.New, msg) }
func Sha1(msg []byte) bytesconv.BytesResult { return sum(sha1.New, msg) }
func Sha224(msg []byte) hashResult { return sum(sha256.New224, msg) }
func Sha224(msg []byte) bytesconv.BytesResult { return sum(sha256.New224, msg) }
func Sha256(msg []byte) hashResult { return sum(sha256.New, msg) }
func Sha256(msg []byte) bytesconv.BytesResult { return sum(sha256.New, msg) }
func Sha384(msg []byte) hashResult { return sum(sha512.New384, msg) }
func Sha384(msg []byte) bytesconv.BytesResult { return sum(sha512.New384, msg) }
func Sha512(msg []byte) hashResult { return sum(sha512.New, msg) }
func Sha512(msg []byte) bytesconv.BytesResult { return sum(sha512.New, msg) }
func Sm3(msg []byte) hashResult { return sum(sm3.New, msg) }
func Sm3(msg []byte) bytesconv.BytesResult { return sum(sm3.New, msg) }
func Murmur3(msg []byte) uint64 {
return murmur3.Sum64(msg)
@ -57,27 +56,9 @@ func Funv64(msg []byte) uint64 {
return h.Sum64()
}
type hashResult []byte
func sum(f func() hash.Hash, msg []byte) hashResult {
func sum(f func() hash.Hash, msg []byte) bytesconv.BytesResult {
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()
}