Function chacha20_poly1305_aead::encrypt [] [src]

pub fn encrypt<W: Write>(key: &[u8], nonce: &[u8], aad: &[u8], input: &[u8], output: &mut W) -> Result<[u8; 16]>

Encrypts a byte slice and returns the authentication tag.

Example

use chacha20_poly1305_aead::encrypt;

let key = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
           17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
let nonce = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let aad = [1, 2, 3, 4];

let plaintext = b"hello, world";

// Vec implements the Write trait
let mut ciphertext = Vec::with_capacity(plaintext.len());

let tag = encrypt(&key, &nonce, &aad, plaintext, &mut ciphertext).unwrap();

assert_eq!(ciphertext, [0xfc, 0x5a, 0x17, 0x82,
    0xab, 0xcf, 0xbc, 0x5d, 0x18, 0x29, 0xbf, 0x97]);
assert_eq!(tag, [0xdb, 0xb7, 0x0d, 0xda, 0xbd, 0xfa, 0x8c, 0xa5,
                 0x60, 0xa2, 0x30, 0x3d, 0xe6, 0x07, 0x92, 0x10]);