-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcrypto.go
40 lines (34 loc) · 993 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
40
package netcode
import (
"crypto/rand"
"golang.org/x/crypto/chacha20poly1305"
)
// Generates random bytes
func RandomBytes(bytes int) ([]byte, error) {
b := make([]byte, bytes)
_, err := rand.Read(b)
return b, err
}
// Generates a random key of KEY_BYTES
func GenerateKey() ([]byte, error) {
return RandomBytes(KEY_BYTES)
}
// Encrypts the message in place with the nonce and key and optional additional buffer
func EncryptAead(message []byte, additional, nonce, key []byte) error {
aead, err := chacha20poly1305.New(key)
if err != nil {
return err
}
aead.Seal(message[:0], nonce, message, additional)
return nil
}
// Decrypts the message with the nonce and key and optional additional buffer returning a copy
// byte slice
func DecryptAead(message []byte, additional, nonce, key []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
message, err = aead.Open(message[:0], nonce, message, additional)
return message, err
}