RbNaCl provides AEAD (Authenticated Encryption with Associated Data) implementations, specifically wrappers for ChaCha20-Poly1305 (both the original and the IETF versions). AEAD constructions encrypt a message and compute an authentication tag for both the encrypted message and optional additional data, ensuring both confidentiality and integrity.
To use an AEAD primitive, you must initialize it with a valid secret key. You then use a nonce (number of bytes defined by the specific implementation) to encrypt or decrypt messages.
Error Handling
RbNaCl::LengthError: Raised if the provided key or nonce does not match the expected byte length for the specific primitive.RbNaCl::CryptoError: Raised during decryption if the ciphertext fails authentication (e.g., if the data was tampered with or the wrong key/nonce was used).
# Example conceptual usage of an AEAD primitive
# Note: Actual class names like RbNaCl::AEAD::Chacha20Poly1305IETF are used for specific implementations
aead = RbNaCl::AEAD::Chacha20Poly1305IETF.new(secret_key)
nonce = RbNaCl::Random.random_bytes(aead.nonce_bytes)
additional_data = "some metadata"
# Encrypt
ciphertext = aead.encrypt(nonce, "my secret message", additional_data)
# Decrypt
decrypted_message = aead.decrypt(nonce, ciphertext, additional_data)