-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
49 lines (44 loc) · 1.05 KB
/
aes.go
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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
)
type AESConfig struct {
Nonce []byte
GCM cipher.AEAD
}
func (c *AESConfig) Encrypt(plaintext []byte) string {
ciphertext := c.GCM.Seal(nil, c.Nonce, plaintext, nil)
return hex.EncodeToString(c.Nonce) + hex.EncodeToString(ciphertext)
}
func (c *AESConfig) Decrypt(ciphertextHex string) (string, error) {
cipherData, _ := hex.DecodeString(ciphertextHex)
nonceSize := 12
nonce, ciphertext := cipherData[:nonceSize], cipherData[nonceSize:]
plaintext, err := c.GCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}
func NewAESConfig() (*AESConfig, error) {
block, err := aes.NewCipher([]byte("0E00894B1D18FFB84E1D2E0DBA5611BB"))
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
}
return &AESConfig{
Nonce: nonce,
GCM: gcm,
}, nil
}