shithub: hugo

Download patch

ref: 0f6c73cf2ef3fa478a635b54652173eefa1f3a17
parent: 01b8bc1b261cef85eb600fa6c2deec07fd7dc4b2
author: digitalcraftsman <[email protected]>
date: Sun Dec 4 08:35:31 EST 2016

tpl: Add sha256 template function

Fixes #2742

--- a/docs/content/templates/functions.md
+++ b/docs/content/templates/functions.md
@@ -768,6 +768,17 @@
 <!-- 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
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -19,6 +19,7 @@
 	"bytes"
 	_md5 "crypto/md5"
 	_sha1 "crypto/sha1"
+	_sha256 "crypto/sha256"
 	"encoding/base64"
 	"encoding/hex"
 	"encoding/json"
@@ -1968,6 +1969,17 @@
 	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 @@
 		"sanitizeurl":  helpers.SanitizeURL,
 		"seq":          helpers.Seq,
 		"sha1":         sha1,
+		"sha256":       sha256,
 		"shuffle":      shuffle,
 		"singularize":  singularize,
 		"slice":        slice,
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -145,6 +145,7 @@
 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 @@
 safeURL: http://gohugo.io
 seq: [1 2 3]
 sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
+sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
 singularize: cat
 slicestr: Bat
 slicestr: Man
@@ -2561,6 +2563,30 @@
 		_, err = sha1(t)
 		if err == nil {
 			t.Error("Expected error from sha1")
+		}
+	}
+}
+
+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")
 		}
 	}
 }