tpl/crypto: Add optional encoding arg to hmac function

Closes #9709
This commit is contained in:
Joe Mooring
2022-03-23 09:48:34 -07:00
committed by Bjørn Erik Pedersen
parent a461e9d01a
commit 94e8a90769
3 changed files with 49 additions and 27 deletions

View File

@@ -69,7 +69,7 @@ func (ns *Namespace) SHA256(in any) (string, error) {
}
// HMAC returns a cryptographic hash that uses a key to sign a message.
func (ns *Namespace) HMAC(h any, k any, m any) (string, error) {
func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) {
ha, err := cast.ToStringE(h)
if err != nil {
return "", err
@@ -105,5 +105,21 @@ func (ns *Namespace) HMAC(h any, k any, m any) (string, error) {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)[:]), nil
var encoding = "hex"
if len(e) > 0 && e[0] != nil {
encoding, err = cast.ToStringE(e[0])
if err != nil {
return "", err
}
}
switch encoding {
case "binary":
return string(mac.Sum(nil)[:]), nil
case "hex":
return hex.EncodeToString(mac.Sum(nil)[:]), nil
default:
return "", fmt.Errorf("%q is not a supported encoding method", encoding)
}
}