Skip to content

Commit

Permalink
Merge pull request #1760 from thevilledev/feature/hmac-sha256-function
Browse files Browse the repository at this point in the history
Add function for HMAC SHA256
  • Loading branch information
roncodingenthusiast authored Jun 21, 2023
2 parents a3e4db1 + 7899935 commit 4d2281b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
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 @@ -1498,6 +1499,20 @@ Takes a string input as an argument, and returns the hex-encoded md5 hash of the
{{ "myString" | md5sum }}
```

### `hmacSHA256Hex`

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

```golang
{{ hmacSHA256Hex "somemessage" "somekey" }}
```

Or with a pipe function

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

### `split`

Splits the given string on the provided separator:
Expand Down
9 changes: 9 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,14 @@ func md5sum(item string) (string, error) {
return fmt.Sprintf("%x", md5.Sum([]byte(item))), nil
}

// hmacSHA256Hex returns the HMAC-SHA256 hash in hexadecimal format of the
// given message by using the provided key
func hmacSHA256Hex(message, key string) (string, error) {
h := hmac.New(sha256.New, []byte(key))
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
key string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Should return the proper string",
args: args{
message: "bladibla",
key: "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.key)
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" "somekey" }}`,
},
nil,
"6116e95f2827172aa6ef8b22b883f6a77e966aefc129c6b8228ebd0aac74e98d",
false,
},
{
"func_datacenters",
&NewTemplateInput{
Expand Down

0 comments on commit 4d2281b

Please sign in to comment.