The `chacha` package provides an implementation of the ChaCha20 Poly1305 authenticated encryption algorithm, designed to be compatible with Node.js's `crypto.createCipheriv()` and `createDecipheriv()` API for AES-GCM mode. It uses the more recent IETF draft for ChaCha20-Poly1305 AEAD, which features a 96-bit nonce, distinct from earlier drafts implemented in systems like BoringSSL (though it offers a 'Legacy Aead' for compatibility). The library supports both a pure JavaScript implementation and optional native bindings for performance in Node.js environments, automatically falling back to pure JS where native bindings are unavailable or explicitly opted out. It exposes APIs for the full AEAD, the ChaCha20 stream cipher, and the Poly1305 message authentication code independently. The current stable version is 2.1.0, and while a specific release cadence isn't published, the active GitHub repository and continuous integration suggest ongoing maintenance and stability.
npm install chachaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the full ChaCha20 Poly1305 Authenticated Encryption and Decryption (AEAD) flow, including setting the key, nonce, Additional Authenticated Data (AAD), and handling the authentication tag. It highlights the correct order of operations for `setAAD`, `update`, `final`, `getAuthTag`, and `setAuthTag`.
Ensure all parties use the same version of the ChaCha20-Poly1305 AEAD. If interoperating with systems using the older draft, use `chacha.AeadLegacy(key, nonce)` for encryption/decryption on both sides.
Always invoke `cipher.setAAD(associatedData)` immediately after creating the cipher/decipher instance and before processing any encrypted or plaintext data. The same AAD must be used for both encryption and decryption.
Ensure `cipher.getAuthTag()` is the last operation performed on the cipher instance after `cipher.final()` has been called to ensure the complete and correct authentication tag is generated.
Always set the correct authentication tag using `decipher.setAuthTag(tag)` after initializing the decipher and setting AAD, but before decrypting any ciphertext data. Handle potential `try...catch` blocks around decryption to gracefully manage authentication failures.
To explicitly use the pure JavaScript implementation in Node.js, `require` the specific browser path: `const chachaPureJs = require('chacha/browser');`. This bypasses the native binding attempt.Verify that the correct authentication tag (obtained from `cipher.getAuthTag()`) is being passed to `decipher.setAuthTag()`. Ensure `setAuthTag` is called before any `update()` operations during decryption.
Ensure that `cipher.setAAD()` and `decipher.setAAD()` (and `decipher.setAuthTag()`) are called immediately after creating the cipher/decipher instance and before any `update()` or `final()` calls.
If in a CommonJS module, use `const chacha = require('chacha');`. If in an ES module, use `import * as chacha from 'chacha';` and then access methods via the `chacha` object (e.g., `chacha.createCipher`).Ensure the key is a 32-byte Buffer (256 bits) and the nonce is a 12-byte Buffer (96 bits). Generate them securely using a cryptographically strong random number generator.