[add] yao.crypto.hmac process
This commit is contained in:
parent
48fb5b202d
commit
382f6aa7d3
3 changed files with 58 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ package crypto
|
|||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/hmac"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/md4"
|
||||
|
|
@ -42,3 +43,13 @@ func Hash(hash crypto.Hash, value string) (string, error) {
|
|||
}
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// Hmac the Keyed-Hash Message Authentication Code (HMAC)
|
||||
func Hmac(hash crypto.Hash, value string, key string) (string, error) {
|
||||
mac := hmac.New(hash.New, []byte(key))
|
||||
_, err := mac.Write([]byte(value))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%x", mac.Sum(nil)), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,25 +8,49 @@ import (
|
|||
)
|
||||
|
||||
func TestMD4(t *testing.T) {
|
||||
// Hash
|
||||
args := []interface{}{"MD4", "123456"}
|
||||
res := gou.NewProcess("yao.crypto.hash", args...).Run()
|
||||
assert.Equal(t, "585028aa0f794af812ee3be8804eb14a", res)
|
||||
|
||||
// HMac
|
||||
args = append(args, "123456")
|
||||
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||
assert.Equal(t, "356f45727db95d65843b2794474d741c", res)
|
||||
}
|
||||
|
||||
func TestMD5(t *testing.T) {
|
||||
// Hash
|
||||
args := []interface{}{"MD5", "123456"}
|
||||
res := gou.NewProcess("yao.crypto.hash", args...).Run()
|
||||
assert.Equal(t, "e10adc3949ba59abbe56e057f20f883e", res)
|
||||
|
||||
// HMac
|
||||
args = append(args, "123456")
|
||||
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||
assert.Equal(t, "30ce71a73bdd908c3955a90e8f7429ef", res)
|
||||
}
|
||||
|
||||
func TestSHA1(t *testing.T) {
|
||||
// Hash
|
||||
args := []interface{}{"SHA1", "123456"}
|
||||
res := gou.NewProcess("yao.crypto.hash", args...).Run()
|
||||
assert.Equal(t, "7c4a8d09ca3762af61e59520943dc26494f8941b", res)
|
||||
|
||||
// HMac
|
||||
args = append(args, "123456")
|
||||
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||
assert.Equal(t, "74b55b6ab2b8e438ac810435e369e3047b3951d0", res)
|
||||
}
|
||||
|
||||
func TestSHA256(t *testing.T) {
|
||||
// Hash
|
||||
args := []interface{}{"SHA256", "123456"}
|
||||
res := gou.NewProcess("yao.crypto.hash", args...).Run()
|
||||
assert.Equal(t, "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", res)
|
||||
|
||||
// HMac
|
||||
args = append(args, "123456")
|
||||
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||
assert.Equal(t, "b8ad08a3a547e35829b821b75370301dd8c4b06bdd7771f9b541a75914068718", res)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
func init() {
|
||||
gou.RegisterProcessHandler("yao.crypto.hash", ProcessHash)
|
||||
gou.RegisterProcessHandler("yao.crypto.hmac", ProcessHmac)
|
||||
}
|
||||
|
||||
// ProcessHash yao.crypto.hash Crypto Hash
|
||||
|
|
@ -28,3 +29,25 @@ func ProcessHash(process *gou.Process) interface{} {
|
|||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// ProcessHmac yao.crypto.hmac Crypto the Keyed-Hash Message Authentication Code (HMAC) Hash
|
||||
// Args[0] string: the hash function name. MD4/MD5/SHA1/SHA224/SHA256/SHA384/SHA512/MD5SHA1/RIPEMD160/SHA3_224/SHA3_256/SHA3_384/SHA3_512/SHA512_224/SHA512_256/BLAKE2s_256/BLAKE2b_256/BLAKE2b_384/BLAKE2b_512
|
||||
// Args[1] string: value
|
||||
// Args[2] string: key
|
||||
func ProcessHmac(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
typ := process.ArgsString(0)
|
||||
value := process.ArgsString(1)
|
||||
key := process.ArgsString(2)
|
||||
|
||||
h, has := hashTypes[typ]
|
||||
if !has {
|
||||
exception.New("%s does not support", 400, typ).Throw()
|
||||
}
|
||||
|
||||
res, err := Hmac(h, value, key)
|
||||
if err != nil {
|
||||
exception.New("%s error: %s value: %s", 400, typ, err, value).Throw()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue