JS-ChaCha20
Pure JavaScript ChaCha20 stream cipher
Abstract
ChaCha20 is a stream cipher designed by D. J. Bernstein. It is a refinement of the Salsa20 algorithm, and it uses a 256-bit key.
ChaCha20 successively calls the ChaCha20 block function, with the same key and nonce, and with successively increasing block counter parameters. ChaCha20 then serializes the resulting state by writing the numbers in little-endian order, creating a keystream block.
Concatenating the keystream blocks from the successive blocks forms a keystream.
The ChaCha20 function then performs an XOR of this keystream with the plaintext.
Alternatively, each keystream block can be XORed with a plaintext block before proceeding to create the next block, saving some memory.
There is no requirement for the plaintext to be an integral multiple of 512 bits. If there is extra keystream from the last block, it is discarded.
The inputs to ChaCha20 are:
- 256-bit key
- 96-bit nonce. In some protocols, this is known as the Initialization Vector
- 32-bit initial counter
- Arbitrary-length plaintext
Implementation derived from RFC7539 ChaCha20 and Poly1305 for IETF Protocols
Install
npm install js-chacha20 --save
Usage
Encrypt message with key and nonce
; const key = Uint8Array...; // 32 bytes keyconst nonce = Uint8Array...; // 12 bytes nonceconst message = Uint8Array...; // some data as bytes array // Encrypt //const encrypt = key nonce; // now encrypt contains bytes array of encrypted message
Decrypt encrypted message with key and nonce
; const key = Uint8Array...; // 32 bytes keyconst nonce = Uint8Array...; // 12 bytes nonceconst encrypt = Uint8Array...; // some data as bytes array // Encrypt //const message = key nonce; // now message contains bytes array of original message
That all. If something happens, Error will be thrown. More examples you can find in tests files.