-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgcm.go
More file actions
30 lines (26 loc) · 726 Bytes
/
gcm.go
File metadata and controls
30 lines (26 loc) · 726 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
package encfile
import (
"crypto/aes"
"crypto/cipher"
)
func gcm(k []byte) cipher.AEAD {
aesBC, err := aes.NewCipher(k[0:32])
if err != nil {
panic(err.Error())
}
ret, err := cipher.NewGCM(aesBC)
if err != nil {
panic(err.Error())
}
return ret
}
// encryptSector data with AES-GCM using key and nonce. No length check on key/nonce is done!
func encryptSector(key, nonce, data []byte) []byte {
algo := gcm(key)
return algo.Seal(nil, nonce[:algo.NonceSize()], data, nil)
}
// decryptSector data with AES-GCM using key and nonce. No length check on key/nonce is done!
func decryptSector(key, nonce, data []byte) ([]byte, error) {
algo := gcm(key)
return algo.Open(nil, nonce[:algo.NonceSize()], data, nil)
}