Skip to content

Commit f07659e

Browse files
authored
Merge pull request #400 from itzik-elayev/master
feature: added sha512sum function
2 parents 98b35c1 + 331c9c9 commit f07659e

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

crypto.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"crypto/rsa"
1515
"crypto/sha1"
1616
"crypto/sha256"
17+
"crypto/sha512"
1718
"crypto/x509"
1819
"crypto/x509/pkix"
1920
"encoding/asn1"
@@ -36,6 +37,11 @@ import (
3637
"golang.org/x/crypto/scrypt"
3738
)
3839

40+
func sha512sum(input string) string {
41+
hash := sha512.Sum512([]byte(input))
42+
return hex.EncodeToString(hash[:])
43+
}
44+
3945
func sha256sum(input string) string {
4046
hash := sha256.Sum256([]byte(input))
4147
return hex.EncodeToString(hash[:])

crypto_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ var (
2626
}
2727
)
2828

29+
func TestSha512Sum(t *testing.T) {
30+
tpl := `{{"abc" | sha512sum}}`
31+
if err := runt(tpl, "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); err != nil {
32+
t.Error(err)
33+
}
34+
}
35+
2936
func TestSha256Sum(t *testing.T) {
3037
tpl := `{{"abc" | sha256sum}}`
3138
if err := runt(tpl, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); err != nil {
3239
t.Error(err)
3340
}
3441
}
42+
3543
func TestSha1Sum(t *testing.T) {
3644
tpl := `{{"abc" | sha1sum}}`
3745
if err := runt(tpl, "a9993e364706816aba3e25717850c26c9cd0d89d"); err != nil {

docs/crypto.md

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ sha256sum "Hello world!"
2121
The above will compute the SHA 256 sum in an "ASCII armored" format that is
2222
safe to print.
2323

24+
## sha512sum
25+
26+
The `sha512sum` function receives a string, and computes it's SHA512 digest.
27+
28+
```
29+
sha512sum "Hello world!"
30+
```
31+
32+
The above will compute the SHA 512 sum in an "ASCII armored" format that is
33+
safe to print.
34+
2435
## adler32sum
2536

2637
The `adler32sum` function receives a string, and computes its Adler-32 checksum.

functions.go

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ var genericMap = map[string]interface{}{
161161
"plural": plural,
162162
"sha1sum": sha1sum,
163163
"sha256sum": sha256sum,
164+
"sha512sum": sha512sum,
164165
"adler32sum": adler32sum,
165166
"toString": strval,
166167

0 commit comments

Comments
 (0)