[add] yao.crypto.hmac base64 encoding
This commit is contained in:
parent
382f6aa7d3
commit
1aa82b7e5e
3 changed files with 28 additions and 4 deletions
|
|
@ -3,6 +3,7 @@ package crypto
|
||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"golang.org/x/crypto/md4"
|
"golang.org/x/crypto/md4"
|
||||||
|
|
@ -45,11 +46,16 @@ func Hash(hash crypto.Hash, value string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hmac the Keyed-Hash Message Authentication Code (HMAC)
|
// Hmac the Keyed-Hash Message Authentication Code (HMAC)
|
||||||
func Hmac(hash crypto.Hash, value string, key string) (string, error) {
|
func Hmac(hash crypto.Hash, value string, key string, encoding ...string) (string, error) {
|
||||||
mac := hmac.New(hash.New, []byte(key))
|
mac := hmac.New(hash.New, []byte(key))
|
||||||
_, err := mac.Write([]byte(value))
|
_, err := mac.Write([]byte(value))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(encoding) > 0 && encoding[0] == "base64" {
|
||||||
|
return base64.StdEncoding.EncodeToString(mac.Sum(nil)), nil
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%x", mac.Sum(nil)), nil
|
return fmt.Sprintf("%x", mac.Sum(nil)), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,3 +54,15 @@ func TestSHA256(t *testing.T) {
|
||||||
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||||
assert.Equal(t, "b8ad08a3a547e35829b821b75370301dd8c4b06bdd7771f9b541a75914068718", res)
|
assert.Equal(t, "b8ad08a3a547e35829b821b75370301dd8c4b06bdd7771f9b541a75914068718", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSHA1Base64(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", "base64")
|
||||||
|
res = gou.NewProcess("yao.crypto.hmac", args...).Run()
|
||||||
|
assert.Equal(t, "dLVbarK45DisgQQ142njBHs5UdA=", res)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ func init() {
|
||||||
// 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[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[1] string: value
|
||||||
func ProcessHash(process *gou.Process) interface{} {
|
func ProcessHash(process *gou.Process) interface{} {
|
||||||
process.ValidateArgNums(1)
|
process.ValidateArgNums(2)
|
||||||
typ := process.ArgsString(0)
|
typ := process.ArgsString(0)
|
||||||
value := process.ArgsString(1)
|
value := process.ArgsString(1)
|
||||||
|
|
||||||
|
|
@ -34,8 +34,9 @@ func ProcessHash(process *gou.Process) interface{} {
|
||||||
// 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[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[1] string: value
|
||||||
// Args[2] string: key
|
// Args[2] string: key
|
||||||
|
// Args[3] string: base64
|
||||||
func ProcessHmac(process *gou.Process) interface{} {
|
func ProcessHmac(process *gou.Process) interface{} {
|
||||||
process.ValidateArgNums(2)
|
process.ValidateArgNums(3)
|
||||||
typ := process.ArgsString(0)
|
typ := process.ArgsString(0)
|
||||||
value := process.ArgsString(1)
|
value := process.ArgsString(1)
|
||||||
key := process.ArgsString(2)
|
key := process.ArgsString(2)
|
||||||
|
|
@ -45,7 +46,12 @@ func ProcessHmac(process *gou.Process) interface{} {
|
||||||
exception.New("%s does not support", 400, typ).Throw()
|
exception.New("%s does not support", 400, typ).Throw()
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := Hmac(h, value, key)
|
encoding := ""
|
||||||
|
if process.NumOfArgs() > 3 {
|
||||||
|
encoding = process.ArgsString(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := Hmac(h, value, key, encoding)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New("%s error: %s value: %s", 400, typ, err, value).Throw()
|
exception.New("%s error: %s value: %s", 400, typ, err, value).Throw()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue