Skip to main content

encrypt

Function encrypt 

Source
pub fn encrypt(
    key: &[u8; 32],
    nonce: &[u8; 12],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>>
Expand description

Encrypt data using ChaCha20-Poly1305 (AEAD)

§Arguments

  • key - 32-byte encryption key
  • nonce - 12-byte nonce (MUST be unique for each encryption with the same key)
  • plaintext - Data to encrypt
  • aad - Additional authenticated data (not encrypted, but authenticated)

§Returns

Ciphertext with authentication tag appended (plaintext.len() + 16 bytes)

§Errors

Returns AionError::EncryptionFailed if encryption fails

§Security

CRITICAL: Never reuse a nonce with the same key. Use generate_nonce() for each encryption.