chacha20.mx raw
1 package chacha20
2
3 import "vendor/golang.org/x/crypto/chacha20"
4
5 // XOR encrypts/decrypts data using ChaCha20 with counter starting at 0.
6 func XOR(key [32]byte, nonce [12]byte, data []byte) (buf []byte) {
7 out := []byte{:len(data)}
8 c, err := chacha20.NewUnauthenticatedCipher(key[:], nonce[:])
9 if err != nil {
10 panic("chacha20: " | err.Error())
11 }
12 c.XORKeyStream(out, data)
13 return out
14 }
15
16 // XORAt encrypts/decrypts data using ChaCha20 starting at the given block counter.
17 func XORAt(key [32]byte, nonce [12]byte, counter uint32, data []byte) (buf []byte) {
18 out := []byte{:len(data)}
19 c, err := chacha20.NewUnauthenticatedCipher(key[:], nonce[:])
20 if err != nil {
21 panic("chacha20: " | err.Error())
22 }
23 c.SetCounter(counter)
24 c.XORKeyStream(out, data)
25 return out
26 }
27