-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for direct key-value pair writing in WritePipelineEnv (#…
- Loading branch information
1 parent
fef16f7
commit fb23269
Showing
5 changed files
with
246 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package encryption | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// Decrypt decrypts base64-encoded data using AES-CFB | ||
func Decrypt(secret, base64CipherText []byte) ([]byte, error) { | ||
cipherText, err := base64.StdEncoding.DecodeString(string(base64CipherText)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode from base64: %w", err) | ||
} | ||
|
||
key := sha256.Sum256(secret) | ||
block, err := aes.NewCipher(key[:]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cipher: %w", err) | ||
} | ||
|
||
if len(cipherText) < aes.BlockSize { | ||
return nil, fmt.Errorf("invalid ciphertext: block size too small") | ||
} | ||
|
||
iv := cipherText[:aes.BlockSize] | ||
cipherText = cipherText[aes.BlockSize:] | ||
|
||
stream := cipher.NewCFBDecrypter(block, iv) | ||
stream.XORKeyStream(cipherText, cipherText) | ||
|
||
return cipherText, nil | ||
} | ||
|
||
// Encrypt encrypts data using AES-CFB and encodes it in base64 | ||
func Encrypt(secret, inBytes []byte) ([]byte, error) { | ||
if len(secret) == 0 { | ||
return nil, fmt.Errorf("failed to create cipher: empty secret") | ||
} | ||
|
||
key := sha256.Sum256(secret) | ||
block, err := aes.NewCipher(key[:]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cipher: %w", err) | ||
} | ||
|
||
cipherText := make([]byte, aes.BlockSize+len(inBytes)) | ||
iv := cipherText[:aes.BlockSize] | ||
if _, err = io.ReadFull(rand.Reader, iv); err != nil { | ||
return nil, fmt.Errorf("failed to init iv: %w", err) | ||
} | ||
|
||
stream := cipher.NewCFBEncrypter(block, iv) | ||
stream.XORKeyStream(cipherText[aes.BlockSize:], inBytes) | ||
|
||
return []byte(base64.StdEncoding.EncodeToString(cipherText)), nil | ||
} |
Oops, something went wrong.