Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for HMAC SHA256 #1760

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ provides the following functions:
- [replaceAll](#replaceall)
- [sha256Hex](#sha256hex)
- [md5sum](#md5sum)
- [hmacSHA256Hex](#hmacSHA256hex)
- [split](#split)
- [splitToMap](#splitToMap)
- [timestamp](#timestamp)
Expand Down Expand Up @@ -1486,6 +1487,20 @@ Takes a string input as an argument, and returns the hex-encoded md5 hash of the
{{ "myString" | md5sum }}
```

### `hmacSHA256Hex`

Takes a secret and a message as string inputs. Returns a hex-encoded HMAC SHA256 hash of the given parameters.

```golang
{{ hmacSHA256Hex "somemessage" "somesecret" }}
```

Or with a pipe function

```golang
{{ "somesecret" | hmacSHA256Hex "somemessage" }}
```

### `split`

Splits the given string on the provided separator:
Expand Down
8 changes: 8 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package template

import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
Expand Down Expand Up @@ -1720,6 +1721,13 @@ func md5sum(item string) (string, error) {
return fmt.Sprintf("%x", md5.Sum([]byte(item))), nil
}

// hmacSHA256Hex returns the HMAC SHA256 hex of a string
func hmacSHA256Hex(message, secret string) (string, error) {
thevilledev marked this conversation as resolved.
Show resolved Hide resolved
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil)), nil
}

// writeToFile writes the content to a file with permissions, username (or UID), group name (or GID),
// and optional flags to select appending mode or add a newline.
//
Expand Down
35 changes: 35 additions & 0 deletions template/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,38 @@ func Test_md5sum(t *testing.T) {
})
}
}

func Test_hmacSHA256Hex(t *testing.T) {
type args struct {
message string
secret string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Should return the proper string",
args: args{
message: "bladibla",
secret: "foobar",
},
want: "82cd4c36fa45a1936e93d005ea2fd008350339bb9246a3ba0c8dfecb9d77155b",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := hmacSHA256Hex(tt.args.message, tt.args.secret)
if (err != nil) != tt.wantErr {
t.Errorf("hmacSHA256Hex() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("hmacSHA256Hex() got = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"replaceAll": replaceAll,
"sha256Hex": sha256Hex,
"md5sum": md5sum,
"hmacSHA256Hex": hmacSHA256Hex,
"timestamp": timestamp,
"toLower": toLower,
"toJSON": toJSON,
Expand Down
9 changes: 9 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ func TestTemplate_Execute(t *testing.T) {
"dGVzdGluZzEyMw==",
false,
},
{
"func_hmacSHA256Hex",
&NewTemplateInput{
Contents: `{{ hmacSHA256Hex "somemessage" "somesecret" }}`,
},
nil,
"64e071cca0eb27a3b12d70efc15c18a52a91d84cf4e91a5943195f1015bf7c34",
false,
},
{
"func_datacenters",
&NewTemplateInput{
Expand Down