-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
31 lines (26 loc) · 828 Bytes
/
key.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
package xcp
import (
"crypto/cipher"
"golang.org/x/crypto/chacha20poly1305"
)
const (
KeyLength = chacha20poly1305.KeySize
nonceLength = chacha20poly1305.NonceSizeX
CipherTextMinLength = nonceLength + chacha20poly1305.Overhead
ptBlockSize = 64 * 1024
ctBlockSize = ptBlockSize + chacha20poly1305.Overhead
)
// SymmetricCipher is a wrapper around the AEAD interface from the golang.org/x/crypto/chacha20poly1305 package.
type SymmetricCipher struct {
aead *cipher.AEAD
}
// New returns a new Cipher instance. If a Cipher instance with the same key has already been created, it will be returned instead.
func New(key []byte) (*SymmetricCipher, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
return &SymmetricCipher{
aead: &aead,
}, nil
}