-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
39 lines (32 loc) · 753 Bytes
/
crypto.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
package e2ee
import (
"crypto/aes"
"crypto/cipher"
)
func decrypt(key []byte, nonce []byte, ciphertext []byte, ad []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCMWithNonceSize(c, len(nonce))
if err != nil {
return nil, err
}
plaintext, err := gcm.Open(nil, nonce, ciphertext, ad)
if err != nil {
return nil, err
}
return plaintext, nil
}
func encrypt(key []byte, nonce []byte, plaintext []byte, ad []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCMWithNonceSize(c, len(nonce))
if err != nil {
return nil, err
}
ciphertext := gcm.Seal(nil, nonce, plaintext, ad)
return ciphertext, nil
}