Skip to content

Commit

Permalink
feat: add gzip and ungzip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince-Chenal committed Sep 13, 2024
1 parent e708470 commit 5185210
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
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 @@ -657,3 +658,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 @@ -430,3 +430,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)
}
}
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ var genericMap = map[string]interface{}{
"encryptAES": encryptAES,
"decryptAES": decryptAES,
"randBytes": randBytes,
"gzip": gzipCompress,
"ungzip": gzipDecompress,

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

0 comments on commit 5185210

Please sign in to comment.