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

tpl: Add sha256 template function #2762

Merged
merged 1 commit into from
Dec 4, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions docs/content/templates/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,17 @@ This can be useful if you want to use Gravatar for generating a unique avatar:
<!-- returns the string "c8b5b0e33d408246e30f53e32b8f7627a7a649d4" -->
```


### sha256

`sha256` hashes the given input and returns its SHA256 checksum.

```html
{{ sha256 "Hello world, gophers!" }}
<!-- returns the string "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46" -->
```


## Internationalization

### i18n
Expand Down
13 changes: 13 additions & 0 deletions tpl/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
_md5 "crypto/md5"
_sha1 "crypto/sha1"
_sha256 "crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -1968,6 +1969,17 @@ func sha1(in interface{}) (string, error) {
return hex.EncodeToString(hash[:]), nil
}

// sha256 hashes the given input and returns its SHA256 checksum
func sha256(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return "", err
}

hash := _sha256.Sum256([]byte(conv))
return hex.EncodeToString(hash[:]), nil
}

// querify encodes the given parameters “URL encoded” form ("bar=baz&foo=quux") sorted by key.
func querify(params ...interface{}) (string, error) {
qs := url.Values{}
Expand Down Expand Up @@ -2099,6 +2111,7 @@ func initFuncMap() {
"sanitizeurl": helpers.SanitizeURL,
"seq": helpers.Seq,
"sha1": sha1,
"sha256": sha256,
"shuffle": shuffle,
"singularize": singularize,
"slice": slice,
Expand Down
26 changes: 26 additions & 0 deletions tpl/template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ safeJS: {{ "(1*2)" | safeJS | safeJS }}
safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
seq: {{ seq 3 }}
sha1: {{ sha1 "Hello world, gophers!" }}
sha256: {{ sha256 "Hello world, gophers!" }}
singularize: {{ "cats" | singularize }}
slicestr: {{slicestr "BatMan" 0 3}}
slicestr: {{slicestr "BatMan" 3}}
Expand Down Expand Up @@ -214,6 +215,7 @@ safeJS: (1*2)
safeURL: http://gohugo.io
seq: [1 2 3]
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
singularize: cat
slicestr: Bat
slicestr: Man
Expand Down Expand Up @@ -2565,6 +2567,30 @@ func TestSHA1(t *testing.T) {
}
}

func TestSHA256(t *testing.T) {
for i, this := range []struct {
input string
expectedHash string
}{
{"Hello world, gophers!", "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46"},
{"Lorem ipsum dolor", "9b3e1beb7053e0f900a674dd1c99aca3355e1275e1b03d3cb1bc977f5154e196"},
} {
result, err := sha256(this.input)
if err != nil {
t.Errorf("sha256 returned error: %s", err)
}

if result != this.expectedHash {
t.Errorf("[%d] sha256: expected '%s', got '%s'", i, this.expectedHash, result)
}

_, err = sha256(t)
if err == nil {
t.Error("Expected error from sha256")
}
}
}

func TestReadFile(t *testing.T) {
viper.Reset()
defer viper.Reset()
Expand Down