Skip to content

Commit

Permalink
Add md5 and sha1 template funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalcraftsman authored and bep committed Mar 10, 2016
1 parent be3519f commit 94c3825
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
27 changes: 27 additions & 0 deletions docs/content/templates/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,33 @@ CJK-like languages.
```


### md5

`md5` hashes the given input and returns its MD5 checksum.

```html
{{ md5 "Hello world, gophers!" }}
<!-- returns the string "b3029f756f98f79e7f1b7f1d1f0dd53b" -->
```

This can be useful if you want to use Gravatar for generating a unique avatar:

```html
<img src="https://www.gravatar.com/avatar/{{ md5 "[email protected]" }}?s=100&d=identicon">
```


### sha1

`sha1` hashes the given input and returns its SHA1 checksum.

```html
{{ sha1 "Hello world, gophers!" }}
<!-- returns the string "c8b5b0e33d408246e30f53e32b8f7627a7a649d4" -->
```



## URLs

### absURL, relURL
Expand Down
27 changes: 27 additions & 0 deletions tpl/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ package tpl

import (
"bytes"
_md5 "crypto/md5"
_sha1 "crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -1501,6 +1504,28 @@ func singularize(in interface{}) (string, error) {
return inflect.Singularize(word), nil
}

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

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

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

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

func init() {
funcMap = template.FuncMap{
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
Expand Down Expand Up @@ -1538,6 +1563,7 @@ func init() {
"lower": func(a string) string { return strings.ToLower(a) },
"lt": lt,
"markdownify": markdownify,
"md5": md5,
"mod": mod,
"modBool": modBool,
"mul": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
Expand All @@ -1556,6 +1582,7 @@ func init() {
"sanitizeURL": helpers.SanitizeURL,
"sanitizeurl": helpers.SanitizeURL,
"seq": helpers.Seq,
"sha1": sha1,
"shuffle": shuffle,
"singularize": singularize,
"slice": slice,
Expand Down
54 changes: 53 additions & 1 deletion tpl/template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ seq: {{ seq 3 }}
sort: {{ slice "B" "C" "A" | sort }}
delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
jsonify: {{ (slice "A" "B" "C") | jsonify }}
md5: {{ md5 "Hello world, gophers!" }}
sha1: {{ sha1 "Hello world, gophers!" }}
`
expected := `chomp: <p>Blockhead</p>
dateFormat: Wednesday, Jan 21, 2015
Expand Down Expand Up @@ -134,6 +136,8 @@ seq: [1 2 3]
sort: [A B C]
delimit: A, B and C
jsonify: ["A","B","C"]
md5: b3029f756f98f79e7f1b7f1d1f0dd53b
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
`

var b bytes.Buffer
Expand All @@ -155,7 +159,7 @@ jsonify: ["A","B","C"]
err = templ.Execute(&b, &data)

if err != nil {
t.Fatal("Gott error on execute", err)
t.Fatal("Got error on execute", err)
}

if b.String() != expected {
Expand Down Expand Up @@ -2056,3 +2060,51 @@ func TestBase64Encode(t *testing.T) {
t.Error("Expected error from base64Encode")
}
}

func TestMD5(t *testing.T) {
for i, this := range []struct {
input string
expectedHash string
}{
{"Hello world, gophers!", "b3029f756f98f79e7f1b7f1d1f0dd53b"},
{"Lorem ipsum dolor", "06ce65ac476fc656bea3fca5d02cfd81"},
} {
result, err := md5(this.input)
if err != nil {
t.Errorf("md5 returned error: %s", err)
}

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

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

func TestSHA1(t *testing.T) {
for i, this := range []struct {
input string
expectedHash string
}{
{"Hello world, gophers!", "c8b5b0e33d408246e30f53e32b8f7627a7a649d4"},
{"Lorem ipsum dolor", "45f75b844be4d17b3394c6701768daf39419c99b"},
} {
result, err := sha1(this.input)
if err != nil {
t.Errorf("sha1 returned error: %s", err)
}

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

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

0 comments on commit 94c3825

Please sign in to comment.