mirror of
https://github.com/cloudflare/redoctober.git
synced 2026-01-07 14:05:47 +00:00
Move {En,De}cryptCBC and MakeRandom to symcrypt.
The symcrypt package now contains common secret-key code that is redefined in a number of packages.
This commit is contained in:
46
symcrypt/symcrypt.go
Normal file
46
symcrypt/symcrypt.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Package symcrypt contains common symmetric encryption functions.
|
||||
package symcrypt
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
)
|
||||
|
||||
// DecryptCBC decrypt bytes using a key and IV with AES in CBC mode.
|
||||
func DecryptCBC(data, iv, key []byte) (decryptedData []byte, err error) {
|
||||
aesCrypt, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ivBytes := append([]byte{}, iv...)
|
||||
|
||||
decryptedData = make([]byte, len(data))
|
||||
aesCBC := cipher.NewCBCDecrypter(aesCrypt, ivBytes)
|
||||
aesCBC.CryptBlocks(decryptedData, data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// EncryptCBC encrypt data using a key and IV with AES in CBC mode.
|
||||
func EncryptCBC(data, iv, key []byte) (encryptedData []byte, err error) {
|
||||
aesCrypt, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ivBytes := append([]byte{}, iv...)
|
||||
|
||||
encryptedData = make([]byte, len(data))
|
||||
aesCBC := cipher.NewCBCEncrypter(aesCrypt, ivBytes)
|
||||
aesCBC.CryptBlocks(encryptedData, data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MakeRandom is a helper that makes a new buffer full of random data.
|
||||
func MakeRandom(length int) ([]byte, error) {
|
||||
bytes := make([]byte, length)
|
||||
_, err := io.ReadFull(rand.Reader, bytes)
|
||||
return bytes, err
|
||||
}
|
||||
Reference in New Issue
Block a user