82 lines
1.8 KiB
Go
Raw Normal View History

2025-03-09 15:52:33 -04:00
package encryption
2025-03-07 21:05:37 -05:00
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"io"
)
2025-03-09 17:09:21 -04:00
//16 24 32
// Encrypt uses AES-GCM to encrypt the given plaintext with the given key. The
// plaintext is sealed with a 12-byte nonce, which is prepended to the ciphertext.
2025-03-09 18:04:03 -04:00
// The nonce adds 28 bytes to the ciphertext, so the total length of the ciphertext
// is the length of the plaintext plus 28 bytes.
2025-03-07 21:05:37 -05:00
func Encrypt(key, plaintext []byte) ([]byte, error) {
2025-03-09 17:09:21 -04:00
switch len(key) {
case 16, 24, 32: // AES-128, AES-192, AES-256
default:
return nil, errors.New("invalid key length")
}
2025-03-07 21:05:37 -05:00
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
return ciphertext, nil
}
2025-03-09 17:09:21 -04:00
// Decrypt uses AES-GCM to decrypt the given ciphertext with the given key. This
// function expects that the first 12 bytes of the ciphertext are the nonce that
// was used to encrypt the plaintext.
2025-03-07 21:05:37 -05:00
func Decrypt(key, ciphertext []byte) ([]byte, error) {
2025-03-09 17:09:21 -04:00
switch len(key) {
case 16, 24, 32: // AES-128, AES-192, AES-256
default:
return nil, errors.New("invalid key length")
}
if len(ciphertext) < 12 {
return nil, errors.New("ciphertext too short")
}
2025-03-07 21:05:37 -05:00
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}