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