ts-chacha20
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

TS-ChaCha20

TypeScript ChaCha20 stream cipher (TypeScript port of thesimj/js-chacha20)

Build Status Standard - JavaScript Style Guide

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 ts-chacha20 --save

Usage

Encrypt message with key and nonce

import Chacha20 from "ts-chacha20";

const key = Uint8Array([...]); // 32 bytes key
const nonce = Uint8Array([...]); // 12 bytes nonce
const message = Uint8Array([...]); // some data as bytes array

// Encrypt //
const encrypt = new Jschacha20(key, nonce).encrypt(message);

// now encrypt contains bytes array of encrypted message

Decrypt encrypted message with key and nonce

import Chacha20 from "ts-chacha20";

const key = Uint8Array([...]); // 32 bytes key
const nonce = Uint8Array([...]); // 12 bytes nonce
const encrypt = Uint8Array([...]); // some data as bytes array

// Encrypt //
const message = new Chacha20(key, nonce).decrypt(encrypt);

// 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.

Package Sidebar

Install

npm i ts-chacha20

Weekly Downloads

507

Version

1.2.0

License

MIT

Unpacked Size

30.6 kB

Total Files

12

Last publish

Collaborators

  • rdub