-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchacha20.go
43 lines (34 loc) · 958 Bytes
/
chacha20.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
43
package crab
import (
"math/rand"
"golang.org/x/crypto/chacha20poly1305"
)
// aeadEncrypt encrypts a message with a one-time key.
func Chacha20AEADEncrypt(plaintext, key []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
nonce := make([]byte, chacha20poly1305.NonceSize)
return aead.Seal(nil, nonce, plaintext, nil), nil
}
func Chacha20AEADDecrypt(ciphertext, key []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
nonce := make([]byte, chacha20poly1305.NonceSize)
return aead.Open(nil, nonce, ciphertext, nil)
}
// GenerateKey aes bit: 16/24/32 chacha: 32
func GenerateKey(bit int) []byte {
return []byte(randStr(bit))
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randStr(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}