|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "crypto/rand" |
| 7 | + "encoding/base64" |
| 8 | + "errors" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// EncPrefix marks a string as AES-256-GCM encrypted |
| 15 | +const EncPrefix = "enc:v1:" |
| 16 | + |
| 17 | +// GetAESKey reads the 32-byte AES key from SYSTEM_AES_KEY env. |
| 18 | +// Returns nil if not set or not exactly 32 bytes. |
| 19 | +func GetAESKey() []byte { |
| 20 | + key := []byte(os.Getenv("SYSTEM_AES_KEY")) |
| 21 | + if len(key) == 32 { |
| 22 | + return key |
| 23 | + } |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +// EncryptAESGCM encrypts plaintext with AES-256-GCM. |
| 28 | +// Returns the original string if empty, already encrypted, or key is nil. |
| 29 | +func EncryptAESGCM(plaintext string, key []byte) (string, error) { |
| 30 | + if plaintext == "" || key == nil { |
| 31 | + return plaintext, nil |
| 32 | + } |
| 33 | + if strings.HasPrefix(plaintext, EncPrefix) { |
| 34 | + return plaintext, nil |
| 35 | + } |
| 36 | + |
| 37 | + block, err := aes.NewCipher(key) |
| 38 | + if err != nil { |
| 39 | + return "", err |
| 40 | + } |
| 41 | + aesgcm, err := cipher.NewGCM(block) |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + |
| 46 | + nonce := make([]byte, aesgcm.NonceSize()) |
| 47 | + if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
| 48 | + return "", err |
| 49 | + } |
| 50 | + |
| 51 | + ciphertext := aesgcm.Seal(nil, nonce, []byte(plaintext), nil) |
| 52 | + combined := append(nonce, ciphertext...) |
| 53 | + return EncPrefix + base64.RawURLEncoding.EncodeToString(combined), nil |
| 54 | +} |
| 55 | + |
| 56 | +// DecryptAESGCM decrypts an AES-256-GCM encrypted string. |
| 57 | +// If the string lacks the enc:v1: prefix, it's treated as legacy plaintext and returned as-is. |
| 58 | +func DecryptAESGCM(encrypted string, key []byte) (string, error) { |
| 59 | + if encrypted == "" || key == nil { |
| 60 | + return encrypted, nil |
| 61 | + } |
| 62 | + if !strings.HasPrefix(encrypted, EncPrefix) { |
| 63 | + return encrypted, nil |
| 64 | + } |
| 65 | + |
| 66 | + data, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(encrypted, EncPrefix)) |
| 67 | + if err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + if len(data) < 12 { |
| 71 | + return "", errors.New("invalid encrypted data: too short") |
| 72 | + } |
| 73 | + |
| 74 | + block, err := aes.NewCipher(key) |
| 75 | + if err != nil { |
| 76 | + return "", err |
| 77 | + } |
| 78 | + aesgcm, err := cipher.NewGCM(block) |
| 79 | + if err != nil { |
| 80 | + return "", err |
| 81 | + } |
| 82 | + |
| 83 | + nonce, ciphertext := data[:aesgcm.NonceSize()], data[aesgcm.NonceSize():] |
| 84 | + plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) |
| 85 | + if err != nil { |
| 86 | + return "", err |
| 87 | + } |
| 88 | + return string(plaintext), nil |
| 89 | +} |
0 commit comments