diff --git a/crypto/crypto.go b/crypto/crypto.go index a0dddcda..96f99954 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -3,6 +3,7 @@ package crypto import ( "crypto" "crypto/hmac" + "encoding/base64" "fmt" "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) -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)) _, err := mac.Write([]byte(value)) if err != nil { 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 } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index a1552942..2a85bbd4 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -54,3 +54,15 @@ func TestSHA256(t *testing.T) { res = gou.NewProcess("yao.crypto.hmac", args...).Run() 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) +} diff --git a/crypto/process.go b/crypto/process.go index ee90980a..235950cb 100644 --- a/crypto/process.go +++ b/crypto/process.go @@ -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[1] string: value func ProcessHash(process *gou.Process) interface{} { - process.ValidateArgNums(1) + process.ValidateArgNums(2) typ := process.ArgsString(0) 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[1] string: value // Args[2] string: key +// Args[3] string: base64 func ProcessHmac(process *gou.Process) interface{} { - process.ValidateArgNums(2) + process.ValidateArgNums(3) typ := process.ArgsString(0) value := process.ArgsString(1) key := process.ArgsString(2) @@ -45,7 +46,12 @@ func ProcessHmac(process *gou.Process) interface{} { 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 { exception.New("%s error: %s value: %s", 400, typ, err, value).Throw() }