-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
40 lines (33 loc) · 899 Bytes
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package tplfuncs
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
htmlTemplate "html/template"
textTemplate "text/template"
)
// HashHelpers returns a text template FuncMap with hash related functions
func HashHelpers() textTemplate.FuncMap {
return textTemplate.FuncMap{
"sha1": sha1Func,
"sha256": sha256Func,
}
}
// HashHelpersHTML returns an HTML template FuncMap with hash related functions
func HashHelpersHTML() htmlTemplate.FuncMap {
return htmlTemplate.FuncMap(HashHelpers())
}
// Doc: `sha1` returns the SHA1 hash of the string given.
func sha1Func(input string) string {
h := sha1.New()
h.Write([]byte(input))
sha := h.Sum(nil)
return hex.EncodeToString(sha)
}
// Doc: `sha256` returns the SHA256 hash of the string given.
func sha256Func(input string) string {
h := sha256.New()
h.Write([]byte(input))
sha := h.Sum(nil)
return hex.EncodeToString(sha)
}