From 0f6c73cf2ef3fa478a635b54652173eefa1f3a17 Mon Sep 17 00:00:00 2001 From: digitalcraftsman Date: Sun, 4 Dec 2016 13:35:31 +0100 Subject: [PATCH] tpl: Add sha256 template function Fixes #2742 --- docs/content/templates/functions.md | 11 +++++++++++ tpl/template_funcs.go | 13 +++++++++++++ tpl/template_funcs_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/docs/content/templates/functions.md b/docs/content/templates/functions.md index 53252ff6a..eaeb3cac4 100644 --- a/docs/content/templates/functions.md +++ b/docs/content/templates/functions.md @@ -768,6 +768,17 @@ This can be useful if you want to use Gravatar for generating a unique avatar: ``` + +### sha256 + +`sha256` hashes the given input and returns its SHA256 checksum. + +```html +{{ sha256 "Hello world, gophers!" }} + +``` + + ## Internationalization ### i18n diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index 747641c04..8f5526378 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -19,6 +19,7 @@ import ( "bytes" _md5 "crypto/md5" _sha1 "crypto/sha1" + _sha256 "crypto/sha256" "encoding/base64" "encoding/hex" "encoding/json" @@ -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{} @@ -2099,6 +2111,7 @@ func initFuncMap() { "sanitizeurl": helpers.SanitizeURL, "seq": helpers.Seq, "sha1": sha1, + "sha256": sha256, "shuffle": shuffle, "singularize": singularize, "slice": slice, diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index d9cfcbfa3..91e13e320 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -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}} @@ -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 @@ -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()