Chacha20poly1305
ChaCha20-Poly1305 (IETF, 12-byte nonce) authenticated encryption, detached tag variant.
Does not require AES-NI and works on all CPUs; for random nonces at scale use xchacha20poly1305 instead.
Always use a strict counter nonce and rotate the key before reaching 2^32 messages.
Summary
Functions
Section titled “Functions”keygen
Section titled “keygen”Chacha20poly1305 . keygen ( ) -> buffer
Generates a random 32-byte ChaCha20-Poly1305 key via libsodium’s CSPRNG.
Returns
| buffer |
encrypt
Section titled “encrypt”Chacha20poly1305 . encrypt ( plaintext , key , nonce , ad ) -> EncryptResult
Encrypts plaintext with key (32 bytes) and nonce (12 bytes).
Returns { cipher, tag } where cipher is the same length as plaintext and tag is 16 bytes.
Parameters
| plaintext: buffer | |
| key: buffer | |
| nonce: buffer | |
| ad: buffer? |
Returns
| EncryptResult |
decrypt
Section titled “decrypt”Chacha20poly1305 . decrypt ( cipher , tag , key , nonce , ad ) -> buffer
Decrypts cipher using its detached tag, key (32 bytes), and nonce (12 bytes).
Returns the plaintext buffer; errors if authentication tag does not verify.
Parameters
| cipher: buffer | |
| tag: buffer | |
| key: buffer | |
| nonce: buffer | |
| ad: buffer? |
Returns
| buffer |