From 331c9c9de56778581aa0ed980c57d7d6248a720e Mon Sep 17 00:00:00 2001 From: Itzhak Alayev Date: Wed, 3 Jul 2024 15:48:03 +0300 Subject: [PATCH] Added sha512sum function --- crypto.go | 6 ++++++ crypto_test.go | 8 ++++++++ docs/crypto.md | 11 +++++++++++ functions.go | 30 +++++++++++++++--------------- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/crypto.go b/crypto.go index 13a5cd55..75fe027e 100644 --- a/crypto.go +++ b/crypto.go @@ -14,6 +14,7 @@ import ( "crypto/rsa" "crypto/sha1" "crypto/sha256" + "crypto/sha512" "crypto/x509" "crypto/x509/pkix" "encoding/asn1" @@ -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[:]) diff --git a/crypto_test.go b/crypto_test.go index 449e7ffd..ac3f43b9 100644 --- a/crypto_test.go +++ b/crypto_test.go @@ -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 { diff --git a/docs/crypto.md b/docs/crypto.md index b889d72a..e3220db0 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -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. diff --git a/functions.go b/functions.go index 57fcec1d..d8a7804e 100644 --- a/functions.go +++ b/functions.go @@ -22,8 +22,7 @@ import ( // // Use this to pass the functions into the template engine: // -// tpl := template.New("foo").Funcs(sprig.FuncMap())) -// +// tpl := template.New("foo").Funcs(sprig.FuncMap())) func FuncMap() template.FuncMap { return HtmlFuncMap() } @@ -159,6 +158,7 @@ var genericMap = map[string]interface{}{ "plural": plural, "sha1sum": sha1sum, "sha256sum": sha256sum, + "sha512sum": sha512sum, "adler32sum": adler32sum, "toString": strval, @@ -336,20 +336,20 @@ var genericMap = map[string]interface{}{ "mustChunk": mustChunk, // Crypto: - "bcrypt": bcrypt, - "htpasswd": htpasswd, - "genPrivateKey": generatePrivateKey, - "derivePassword": derivePassword, - "buildCustomCert": buildCustomCertificate, - "genCA": generateCertificateAuthority, - "genCAWithKey": generateCertificateAuthorityWithPEMKey, - "genSelfSignedCert": generateSelfSignedCertificate, + "bcrypt": bcrypt, + "htpasswd": htpasswd, + "genPrivateKey": generatePrivateKey, + "derivePassword": derivePassword, + "buildCustomCert": buildCustomCertificate, + "genCA": generateCertificateAuthority, + "genCAWithKey": generateCertificateAuthorityWithPEMKey, + "genSelfSignedCert": generateSelfSignedCertificate, "genSelfSignedCertWithKey": generateSelfSignedCertificateWithPEMKey, - "genSignedCert": generateSignedCertificate, - "genSignedCertWithKey": generateSignedCertificateWithPEMKey, - "encryptAES": encryptAES, - "decryptAES": decryptAES, - "randBytes": randBytes, + "genSignedCert": generateSignedCertificate, + "genSignedCertWithKey": generateSignedCertificateWithPEMKey, + "encryptAES": encryptAES, + "decryptAES": decryptAES, + "randBytes": randBytes, // UUIDs: "uuidv4": uuidv4,