What is @stablelib/chacha20poly1305?
@stablelib/chacha20poly1305 is a JavaScript implementation of the ChaCha20-Poly1305 authenticated encryption algorithm. It is part of the StableLib collection of cryptographic libraries, which are designed to be secure, fast, and easy to use.
What are @stablelib/chacha20poly1305's main functionalities?
Encryption
This code demonstrates how to encrypt a message using the ChaCha20-Poly1305 algorithm. A 32-byte key and a 12-byte nonce are used to encrypt the message 'Hello, world!'.
const { ChaCha20Poly1305 } = require('@stablelib/chacha20poly1305');
const key = new Uint8Array(32); // 32-byte key
const nonce = new Uint8Array(12); // 12-byte nonce
const message = new TextEncoder().encode('Hello, world!');
const aead = new ChaCha20Poly1305(key);
const encrypted = aead.seal(nonce, message);
console.log(encrypted);
Decryption
This code demonstrates how to decrypt a message using the ChaCha20-Poly1305 algorithm. The same 32-byte key and 12-byte nonce used for encryption are required to decrypt the message.
const { ChaCha20Poly1305 } = require('@stablelib/chacha20poly1305');
const key = new Uint8Array(32); // 32-byte key
const nonce = new Uint8Array(12); // 12-byte nonce
const encrypted = new Uint8Array([...]); // Encrypted message
const aead = new ChaCha20Poly1305(key);
const decrypted = aead.open(nonce, encrypted);
console.log(new TextDecoder().decode(decrypted));
Authenticated Encryption with Additional Data (AEAD)
This code demonstrates how to perform authenticated encryption with additional data (AEAD) using the ChaCha20-Poly1305 algorithm. The additional data is included in the authentication process but is not encrypted.
const { ChaCha20Poly1305 } = require('@stablelib/chacha20poly1305');
const key = new Uint8Array(32); // 32-byte key
const nonce = new Uint8Array(12); // 12-byte nonce
const message = new TextEncoder().encode('Hello, world!');
const additionalData = new TextEncoder().encode('Additional data');
const aead = new ChaCha20Poly1305(key);
const encrypted = aead.seal(nonce, message, additionalData);
console.log(encrypted);
Other packages similar to @stablelib/chacha20poly1305
tweetnacl
TweetNaCl is a cryptographic library that provides a variety of cryptographic functions, including the ChaCha20-Poly1305 authenticated encryption algorithm. It is known for its simplicity and small size. Compared to @stablelib/chacha20poly1305, TweetNaCl offers a broader range of cryptographic primitives but may not be as optimized for performance.
libsodium
Libsodium is a widely-used cryptographic library that provides a comprehensive set of cryptographic functions, including ChaCha20-Poly1305. It is known for its high performance and security. Compared to @stablelib/chacha20poly1305, Libsodium offers more features and is available in multiple languages, but it may be more complex to use.
crypto-js
Crypto-JS is a popular JavaScript library that provides a variety of cryptographic algorithms, including AES, SHA, and HMAC. While it does not natively support ChaCha20-Poly1305, it offers a wide range of other cryptographic functions. Compared to @stablelib/chacha20poly1305, Crypto-JS is more general-purpose but lacks specific support for ChaCha20-Poly1305.