Skip to content

Commit

Permalink
hmac: Add support to return base64 encoded HMAC. (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
fxb authored Jul 8, 2024
1 parent f04f972 commit 2b4c878
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions runtime/modules/hmac/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"hash"
"sync"
Expand Down Expand Up @@ -47,11 +48,12 @@ func LoadModule() (starlark.StringDict, error) {
func fnHmac(hashFunc func() hash.Hash) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
return func(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
key starlark.Value
s starlark.String
binary bool = false
key starlark.Value
s starlark.String
binary bool = false
encoding starlark.String = "hex"
)
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "key", &key, "s", &s, "binary?", &binary); err != nil {
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "key", &key, "s", &s, "binary?", &binary, "encoding?", &encoding); err != nil {
return nil, err
}

Expand All @@ -72,8 +74,10 @@ func fnHmac(hashFunc func() hash.Hash) func(*starlark.Thread, *starlark.Builtin,
}

digest := h.Sum(nil)
if binary {
if binary || encoding == "binary" {
return starlark.Bytes(digest), nil
} else if encoding == "base64" {
return starlark.String(base64.StdEncoding.EncodeToString(digest)), nil
}
return starlark.String(fmt.Sprintf("%x", digest)), nil
}
Expand Down

0 comments on commit 2b4c878

Please sign in to comment.