-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.go
More file actions
103 lines (84 loc) · 2.57 KB
/
aes.go
File metadata and controls
103 lines (84 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
)
// randReader is the source of randomness for nonce generation. Overridable in
// tests to exercise the io.ReadFull error path; production code always uses
// crypto/rand.Reader.
var randReader io.Reader = rand.Reader
// ErrDecrypt is returned when decryption fails.
type ErrDecrypt struct {
Cause error
}
func (e *ErrDecrypt) Error() string {
return fmt.Sprintf("aes-gcm decrypt failed: %v", e.Cause)
}
func (e *ErrDecrypt) Unwrap() error { return e.Cause }
// ErrEncrypt is returned when encryption fails.
type ErrEncrypt struct {
Cause error
}
func (e *ErrEncrypt) Error() string {
return fmt.Sprintf("aes-gcm encrypt failed: %v", e.Cause)
}
func (e *ErrEncrypt) Unwrap() error { return e.Cause }
// ParseAESKey decodes a 64-character hex string into a 32-byte key.
func ParseAESKey(hexKey string) ([]byte, error) {
key, err := hex.DecodeString(hexKey)
if err != nil {
return nil, fmt.Errorf("invalid AES key hex: %w", err)
}
if len(key) != 32 {
return nil, fmt.Errorf("AES key must be 32 bytes, got %d", len(key))
}
return key, nil
}
// Encrypt encrypts plaintext using AES-256-GCM and returns a base64url-encoded string.
// Format: base64(nonce || ciphertext || tag)
func Encrypt(key []byte, plaintext string) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", &ErrEncrypt{Cause: err}
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", &ErrEncrypt{Cause: err}
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(randReader, nonce); err != nil {
return "", &ErrEncrypt{Cause: err}
}
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
return base64.URLEncoding.EncodeToString(ciphertext), nil
}
// Decrypt decodes and decrypts a base64url-encoded ciphertext produced by Encrypt.
func Decrypt(key []byte, encoded string) (string, error) {
data, err := base64.URLEncoding.DecodeString(encoded)
if err != nil {
return "", &ErrDecrypt{Cause: fmt.Errorf("base64 decode: %w", err)}
}
block, err := aes.NewCipher(key)
if err != nil {
return "", &ErrDecrypt{Cause: err}
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", &ErrDecrypt{Cause: err}
}
nonceSize := gcm.NonceSize()
if len(data) < nonceSize {
return "", &ErrDecrypt{Cause: fmt.Errorf("ciphertext too short")}
}
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", &ErrDecrypt{Cause: err}
}
return string(plaintext), nil
}