Skip to content

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.

Chacha20poly1305 . keygen ( )  -> buffer

Generates a random 32-byte ChaCha20-Poly1305 key via libsodium’s CSPRNG.

Returns

buffer
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
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