Skip to content

Commit

Permalink
Merge pull request #400 from itzik-elayev/master
Browse files Browse the repository at this point in the history
feature: added sha512sum function
  • Loading branch information
mattfarina authored Aug 29, 2024
2 parents 98b35c1 + 331c9c9 commit f07659e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
Expand All @@ -36,6 +37,11 @@ import (
"golang.org/x/crypto/scrypt"
)

func sha512sum(input string) string {
hash := sha512.Sum512([]byte(input))
return hex.EncodeToString(hash[:])
}

func sha256sum(input string) string {
hash := sha256.Sum256([]byte(input))
return hex.EncodeToString(hash[:])
Expand Down
8 changes: 8 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ var (
}
)

func TestSha512Sum(t *testing.T) {
tpl := `{{"abc" | sha512sum}}`
if err := runt(tpl, "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); err != nil {
t.Error(err)
}
}

func TestSha256Sum(t *testing.T) {
tpl := `{{"abc" | sha256sum}}`
if err := runt(tpl, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); err != nil {
t.Error(err)
}
}

func TestSha1Sum(t *testing.T) {
tpl := `{{"abc" | sha1sum}}`
if err := runt(tpl, "a9993e364706816aba3e25717850c26c9cd0d89d"); err != nil {
Expand Down
11 changes: 11 additions & 0 deletions docs/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ sha256sum "Hello world!"
The above will compute the SHA 256 sum in an "ASCII armored" format that is
safe to print.

## sha512sum

The `sha512sum` function receives a string, and computes it's SHA512 digest.

```
sha512sum "Hello world!"
```

The above will compute the SHA 512 sum in an "ASCII armored" format that is
safe to print.

## adler32sum

The `adler32sum` function receives a string, and computes its Adler-32 checksum.
Expand Down
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ var genericMap = map[string]interface{}{
"plural": plural,
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"sha512sum": sha512sum,
"adler32sum": adler32sum,
"toString": strval,

Expand Down

0 comments on commit f07659e

Please sign in to comment.