-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaes.go
42 lines (33 loc) · 927 Bytes
/
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
package main
import (
"crypto/aes"
"crypto/cipher"
)
// #############################################################################
func getCipher(key []byte) cipher.AEAD {
block, err := aes.NewCipher(key)
Panic(err)
AES_GCM, err := cipher.NewGCM(block)
Panic(err)
return AES_GCM
}
func getNonce(key []byte) []byte {
return SHA256(key)[:12] // GCM requires a 12 byte nonce
}
func encrypt(pt []byte, key []byte) []byte {
AES_GCM := getCipher(key)
nonce := getNonce(key)
return AES_GCM.Seal(nil, nonce, pt, nil)
}
func decrypt(ct []byte, key []byte) ([]byte, error) {
AES_GCM := getCipher(key)
nonce := getNonce(key)
return AES_GCM.Open(nil, nonce, ct, nil)
}
// #############################################################################
func AEAD_Encrypt(pt []byte, key []byte) []byte {
return encrypt(pt, key)
}
func AEAD_Decrypt(ct []byte, key []byte) ([]byte, error) {
return decrypt(ct, key)
}