mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
01b8bc1b26
commit
0f6c73cf2e
3 changed files with 50 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue