Add algorithm parameter to HmacWith function

This commit is contained in:
Max 2024-01-27 10:17:48 +08:00
parent 12e12c9536
commit baab49be52
2 changed files with 26 additions and 2 deletions

View file

@ -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
}
})
}
}

View file

@ -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]