mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
hash by name
This commit is contained in:
22
hash/hash.go
22
hash/hash.go
@ -5,8 +5,10 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
"github.com/charlienet/go-mixed/bytesconv"
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
@ -14,6 +16,26 @@ import (
|
|||||||
"github.com/tjfoc/gmsm/sm3"
|
"github.com/tjfoc/gmsm/sm3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HashFunc func([]byte) bytesconv.BytesResult
|
||||||
|
|
||||||
|
var hashFuncs = map[string]HashFunc{
|
||||||
|
"MD5": Md5,
|
||||||
|
"SHA1": Sha1,
|
||||||
|
"SHA224": Sha224,
|
||||||
|
"SHA256": Sha256,
|
||||||
|
"SHA384": Sha384,
|
||||||
|
"SHA512": Sha512,
|
||||||
|
"SM3": Sm3,
|
||||||
|
}
|
||||||
|
|
||||||
|
func ByName(name string) (HashFunc, error) {
|
||||||
|
if f, ok := hashFuncs[strings.ToUpper(name)]; ok {
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("Unsupported hash functions")
|
||||||
|
}
|
||||||
|
|
||||||
func Md5(msg []byte) bytesconv.BytesResult { return sum(md5.New, msg) }
|
func Md5(msg []byte) bytesconv.BytesResult { return sum(md5.New, msg) }
|
||||||
|
|
||||||
func Sha1(msg []byte) bytesconv.BytesResult { return sum(sha1.New, msg) }
|
func Sha1(msg []byte) bytesconv.BytesResult { return sum(sha1.New, msg) }
|
||||||
|
22
hmac/hmac.go
22
hmac/hmac.go
@ -6,12 +6,34 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/charlienet/go-mixed/bytesconv"
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
"github.com/tjfoc/gmsm/sm3"
|
"github.com/tjfoc/gmsm/sm3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HMacFunc func(key, msg []byte) bytesconv.BytesResult
|
||||||
|
|
||||||
|
var hmacFuncs = map[string]HMacFunc{
|
||||||
|
"MD5": Md5,
|
||||||
|
"SHA1": Sha1,
|
||||||
|
"SHA224": Sha224,
|
||||||
|
"SHA256": Sha256,
|
||||||
|
"SHA384": Sha384,
|
||||||
|
"SHA512": Sha512,
|
||||||
|
"SM3": Sm3,
|
||||||
|
}
|
||||||
|
|
||||||
|
func ByName(name string) (HMacFunc, error) {
|
||||||
|
if f, ok := hmacFuncs[strings.ToUpper(name)]; ok {
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("Unsupported hash functions")
|
||||||
|
}
|
||||||
|
|
||||||
func Md5(key, msg []byte) bytesconv.BytesResult { return sum(md5.New, key, msg) }
|
func Md5(key, msg []byte) bytesconv.BytesResult { return sum(md5.New, key, msg) }
|
||||||
|
|
||||||
func Sha1(key, msg []byte) bytesconv.BytesResult { return sum(sha1.New, key, msg) }
|
func Sha1(key, msg []byte) bytesconv.BytesResult { return sum(sha1.New, key, msg) }
|
||||||
|
Reference in New Issue
Block a user