chacha20.go 471 B

123456789101112131415161718192021
  1. package crypto
  2. import (
  3. "crypto/cipher"
  4. "github.com/aead/chacha20"
  5. )
  6. // NewChaCha20Stream creates a new Chacha/20 cipher stream. Caller must ensure that key is 32-bytes long and iv is either 8 or 12 bytes.
  7. func NewChaCha20Stream(key []byte, iv []byte) cipher.Stream {
  8. var keyArray [32]byte
  9. var nonce [12]byte
  10. copy(keyArray[:], key)
  11. switch len(iv) {
  12. case 8:
  13. copy(nonce[4:], iv)
  14. case 12:
  15. copy(nonce[:], iv)
  16. }
  17. return chacha20.NewCipher(&nonce, &keyArray)
  18. }