Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add md5sum hash function #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/ed25519"
"crypto/elliptic"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
Expand Down Expand Up @@ -51,6 +52,11 @@ func adler32sum(input string) string {
return fmt.Sprintf("%d", hash)
}

func md5sum(input string) string {
hash := md5.Sum([]byte(input))
return hex.EncodeToString(hash[:])
}

func bcrypt(input string) string {
hash, err := bcrypt_lib.GenerateFromPassword([]byte(input), bcrypt_lib.DefaultCost)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func TestAdler32Sum(t *testing.T) {
}
}

func TestMd5Sum(t *testing.T) {
tpl := `{{"abc" | md5sum}}`
if err := runt(tpl, "900150983cd24fb0d6963f7d28e17f72"); err != nil {
t.Error(err)
}
}

func TestBcrypt(t *testing.T) {
out, err := runRaw(`{{"abc" | bcrypt}}`, nil)
if 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.

## md5sum

The `md5sum` function receives a string, and computes it's MD5 digest.

```
md5sum "Hello world!"
```

The above will compute the MD5 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 @@ -160,6 +160,7 @@ var genericMap = map[string]interface{}{
"sha1sum": sha1sum,
"sha256sum": sha256sum,
"adler32sum": adler32sum,
"md5sum": md5sum,
"toString": strval,

// Wrap Atoi to stop errors.
Expand Down