Skip to content

Commit

Permalink
feat: add key as param
Browse files Browse the repository at this point in the history
  • Loading branch information
andygeiss committed Dec 20, 2024
1 parent bd379b1 commit 4de9136
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 4 additions & 2 deletions security/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

func TestDecrypt(t *testing.T) {
key := security.GenerateKey()
plaintext := []byte("test decryption data")
ciphertext, key := security.Encrypt(plaintext)
ciphertext := security.Encrypt(plaintext, key)
decrypted, err := security.Decrypt(ciphertext, key)
assert.That(t, "err must be nil", err == nil, true)
assert.That(t, "decrypted text must match", decrypted, plaintext)
Expand All @@ -23,8 +24,9 @@ func TestDecrypt_Malformed_Ciphertext(t *testing.T) {
}

func TestDecrypt_Invalid_Key(t *testing.T) {
key := security.GenerateKey()
plaintext := []byte("test invalid key case")
ciphertext, _ := security.Encrypt(plaintext)
ciphertext := security.Encrypt(plaintext, key)
invalidKey := security.GenerateKey()
_, err := security.Decrypt(ciphertext, invalidKey)
assert.That(t, "err must not be nil", err != nil, true)
Expand Down
14 changes: 2 additions & 12 deletions security/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@ import (
"io"
)

// GenerateKey generates a 256-bit (32-byte) random key for AES encryption.
// It uses a cryptographically secure random number generator.
func GenerateKey() [32]byte {
var key [32]byte
_, _ = io.ReadFull(rand.Reader, key[:])
return key
}

// Encrypt takes an input byte slice (plaintext) and encrypts it using AES-GCM.
// It returns the encrypted data (ciphertext) and the key used for encryption.
func Encrypt(plaintext []byte) (ciphertext []byte, key [32]byte) {
// Generate a random 256-bit AES key.
key = GenerateKey()
func Encrypt(plaintext []byte, key [32]byte) (ciphertext []byte) {
// Create a new AES cipher block using the generated key.
block, _ := aes.NewCipher(key[:])
// Create a new GCM (Galois/Counter Mode) cipher based on the AES block cipher.
Expand All @@ -30,5 +20,5 @@ func Encrypt(plaintext []byte) (ciphertext []byte, key [32]byte) {
_, _ = io.ReadFull(rand.Reader, nonce)
// Encrypt the input data using GCM, appending the nonce to the ciphertext.
// The nonce is necessary for decryption.
return gcm.Seal(nonce, nonce, plaintext, nil), key
return gcm.Seal(nonce, nonce, plaintext, nil)
}
3 changes: 2 additions & 1 deletion security/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func TestGenerateKey(t *testing.T) {
}

func TestEncrypt(t *testing.T) {
key := security.GenerateKey()
plaintext := []byte("Alice and Bob")
ciphertext, key := security.Encrypt(plaintext)
ciphertext := security.Encrypt(plaintext, key)
block, _ := aes.NewCipher(key[:])
gcm, _ := cipher.NewGCM(block)
nonceSize := gcm.NonceSize()
Expand Down
14 changes: 14 additions & 0 deletions security/generate_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package security

import (
"crypto/rand"
"io"
)

// GenerateKey generates a 256-bit (32-byte) random key for AES encryption.
// It uses a cryptographically secure random number generator.
func GenerateKey() [32]byte {
var key [32]byte
_, _ = io.ReadFull(rand.Reader, key[:])
return key
}

0 comments on commit 4de9136

Please sign in to comment.