-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathaes.go
More file actions
39 lines (36 loc) · 759 Bytes
/
aes.go
File metadata and controls
39 lines (36 loc) · 759 Bytes
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
package bot
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
)
func AesDecrypt(secret, b []byte) ([]byte, error) {
aes, err := aes.NewCipher(secret)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(aes)
if err != nil {
return nil, err
}
nonce := b[:aead.NonceSize()]
cipher := b[aead.NonceSize():]
return aead.Open(nil, nonce, cipher, nil)
}
func AesEncrypt(secret, b []byte) ([]byte, error) {
aes, err := aes.NewCipher(secret)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(aes)
if err != nil {
return nil, err
}
nonce := make([]byte, aead.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return nil, err
}
cipher := aead.Seal(nil, nonce, b, nil)
return append(nonce, cipher...), nil
}