mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
hash
This commit is contained in:
24
hmac/hmac.go
24
hmac/hmac.go
@ -1,6 +1,7 @@
|
||||
package hmac
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
@ -26,6 +27,29 @@ var hmacFuncs = map[string]HMacFunc{
|
||||
"SM3": Sm3,
|
||||
}
|
||||
|
||||
type hashComparer struct {
|
||||
key []byte
|
||||
hashFunc HMacFunc
|
||||
}
|
||||
|
||||
func New(fname string, key []byte) (*hashComparer, error) {
|
||||
f, err := ByName(fname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &hashComparer{
|
||||
key: key,
|
||||
hashFunc: f,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *hashComparer) Verify(msg, target []byte) bool {
|
||||
ret := c.hashFunc(c.key, msg)
|
||||
|
||||
return bytes.Compare(ret.Bytes(), target) == 0
|
||||
}
|
||||
|
||||
func ByName(name string) (HMacFunc, error) {
|
||||
if f, ok := hmacFuncs[strings.ToUpper(name)]; ok {
|
||||
return f, nil
|
||||
|
Reference in New Issue
Block a user