address pr remarks

This commit is contained in:
Fabio Bozzo
2024-11-04 19:11:25 +01:00
parent 76c015e78b
commit d21c17c4ca
3 changed files with 19 additions and 10 deletions

View File

@@ -9,10 +9,11 @@ import (
)
var ErrShortCipherText = errors.New("ciphertext too short")
var ErrNoEncryptionKey = errors.New("encryption key is required")
func EncryptWithAESKey(data, key []byte) ([]byte, error) {
if key == nil {
return data, nil
return data, ErrNoEncryptionKey
}
block, err := aes.NewCipher(key)
@@ -35,7 +36,7 @@ func EncryptWithAESKey(data, key []byte) ([]byte, error) {
func DecryptStringWithAESKey(data, key []byte) ([]byte, error) {
if key == nil {
return data, nil
return data, ErrNoEncryptionKey
}
block, err := aes.NewCipher(key)

View File

@@ -3,6 +3,7 @@ package crypto
import (
"bytes"
"crypto/rand"
"fmt"
"testing"
"github.com/stretchr/testify/require"
@@ -28,10 +29,10 @@ func TestAESEncryption(t *testing.T) {
wantErr: false,
},
{
name: "nil key returns original data",
name: "nil key returns error",
data: []byte("hello world"),
key: nil,
wantErr: false,
wantErr: true,
},
{
name: "empty data",
@@ -59,6 +60,8 @@ func TestAESEncryption(t *testing.T) {
}
require.NoError(t, err)
fmt.Println(string(encrypted))
decrypted, err := DecryptStringWithAESKey(encrypted, tt.key)
require.NoError(t, err)
@@ -98,6 +101,12 @@ func TestDecryptionErrors(t *testing.T) {
key: key,
errMsg: "message authentication failed",
},
{
name: "missing key",
data: []byte("<22>`M<><4D><EFBFBD>l\u001AIF<49>\u0012<31><32><EFBFBD>=h<>?<3F>c<EFBFBD> <20><>\u0012<31><32><EFBFBD><EFBFBD>\u001C<31>\u0018Ƽ(g"),
key: nil,
errMsg: "encryption key is required",
},
}
for _, tt := range tests {