diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index e4342ca7..ba1a5052 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -166,6 +166,7 @@ func TestHmacWith(t *testing.T) { name string option *hmacOption hash crypto.Hash + algo string value string key string wantErr bool @@ -178,6 +179,7 @@ func TestHmacWith(t *testing.T) { outputEncoding: "hex", }, hash: crypto.SHA256, + algo: "SHA256", value: valuehex, key: keyhex, wantErr: false, @@ -192,6 +194,7 @@ func TestHmacWith(t *testing.T) { hash: crypto.SHA256, value: valuebase64, key: keybase64, + algo: "SHA1", wantErr: false, }, { @@ -221,4 +224,25 @@ func TestHmacWith(t *testing.T) { } }) } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + option := map[string]interface{}{} + if tt.option != nil { + option = map[string]interface{}{ + "key": tt.option.keyEncoding, + "value": tt.option.valueEncoding, + "output": tt.option.outputEncoding, + "algo": tt.algo, + } + } + args := []interface{}{option, tt.value, tt.key} + _, err := process.New("crypto.HmacWith", args...).Exec() + if (err != nil) != tt.wantErr { + t.Errorf("HmacWith() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } } diff --git a/crypto/process.go b/crypto/process.go index f6e5ab81..b0106ac1 100644 --- a/crypto/process.go +++ b/crypto/process.go @@ -75,7 +75,7 @@ func ProcessHmac(process *process.Process) interface{} { } // ProcessHmacWith yao.crypto.hmac Crypto the Keyed-Hash Message Authentication Code (HMAC) Hash -// Args[0] map: option {"key": "base64", "value": "base64", "output": "base64"} // hex/base64 +// Args[0] map: option {"key": "base64", "value": "base64", "output": "base64", "algo": "SHA256"} // hex/base64 // Args[1] string: value // Args[2] string: key func ProcessHmacWith(process *process.Process) interface{} { @@ -94,7 +94,7 @@ func ProcessHmacWith(process *process.Process) interface{} { if v, has := option["output"].(string); has { o.outputEncoding = v } - if v, has := option["type"].(string); has { + if v, has := option["algo"].(string); has && v != "" { typ = v } h, has := HashTypes[typ]