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

feat: add gzip and ungzip functions #379

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sprig

import (
"bytes"
"compress/gzip"
"crypto"
"crypto/aes"
"crypto/cipher"
Expand Down Expand Up @@ -651,3 +652,37 @@ func decryptAES(password string, crypt64 string) (string, error) {

return string(decrypted[:len(decrypted)-int(decrypted[len(decrypted)-1])]), nil
}

func gzipCompress(input string) (string, error) {
var buffer bytes.Buffer
gzipWriter := gzip.NewWriter(&buffer)
_, err := gzipWriter.Write([]byte(input))
if err != nil {
return "", err
}

if err := gzipWriter.Close(); err != nil {
return "", err
}

return buffer.String(), nil
}

func gzipDecompress(input string) (string, error) {
gzipReader, err := gzip.NewReader(bytes.NewReader([]byte(input)))
if err != nil {
return "", err
}

var buffer bytes.Buffer
_, err = buffer.ReadFrom(gzipReader)
if err != nil {
return "", err
}

if err := gzipReader.Close(); err != nil {
return "", err
}

return buffer.String(), nil
}
7 changes: 7 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,10 @@ func TestEncryptDecryptAES(t *testing.T) {
t.Error(err)
}
}

func TestCompressDecompressGzip(t *testing.T) {
tpl := `{{"plaintext" | gzip | ungzip }}`
if err := runt(tpl, "plaintext"); err != nil {
t.Error(err)
}
}
31 changes: 16 additions & 15 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -336,20 +335,22 @@ 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,
"gzip": gzipCompress,
"ungzip": gzipDecompress,

// UUIDs:
"uuidv4": uuidv4,
Expand Down