-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.go
52 lines (47 loc) · 1.2 KB
/
security.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
50
51
52
package utils
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"os"
)
type Utils struct {
aesKey []byte
}
func NewUtils() *Utils {
key := os.Getenv("AES_KEY")
if len(key) != 32 {
panic("AES key must be exactly 32 bytes (256 bits) long")
}
return &Utils{
aesKey: []byte(key),
}
}
// Encrypt encrypts the given data using AES encryption.
func (u *Utils) Encrypt(data []byte) (string, error) {
block, err := aes.NewCipher(u.aesKey)
if err != nil {
return "", err
}
cipherText := make([]byte, aes.BlockSize+len(data))
iv := cipherText[:aes.BlockSize]
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(cipherText[aes.BlockSize:], data)
return base64.StdEncoding.EncodeToString(cipherText), nil
}
// Decrypt decrypts the given data using AES encryption.
func (u *Utils) Decrypt(data string) ([]byte, error) {
cipherText, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(u.aesKey)
if err != nil {
return nil, err
}
plainText := make([]byte, len(cipherText)-aes.BlockSize)
iv := cipherText[:aes.BlockSize]
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plainText, cipherText[aes.BlockSize:])
return plainText, nil
}