ChaCha stream cipher implemented in JavaScript
UNLICENSE License
This is an implementation of the ChaCha stream cipher in JavaScript.
The only function in the API is a constructor that accepts an ArrayBuffer key and ArrayBuffer IV, returning a function that generates the stream 64 bytes at a time. The stream itself is a 64-byte ArrayBuffer. This ArrayBuffer is reused between generator calls.
/* Initialization */
let key = new ArrayBuffer(CHACHA_KEYSIZE);
let iv = new ArrayBuffer(CHACHA_IVSIZE);
// ... set bytes of key and IV ...
let cipher = new ChaCha(key, iv);
/* Encryption / Decryption */
let block = new Uint8Array(cipher());
// ... consume the output ...
cipher(); // fill the buffer with more output
// ... consume the output ...