Function chacha20_poly1305_aead::decrypt [] [src]

pub fn decrypt<W: Write>(key: &[u8], nonce: &[u8], aad: &[u8], input: &[u8], tag: &[u8], output: &mut W) -> Result<(), DecryptError>

Verifies the authentication tag and decrypts a byte slice.

If the tag does not match, this function produces no output and returns Err(DecryptError::TagMismatch).

Example

use chacha20_poly1305_aead::decrypt;

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 ciphertext = [0xfc, 0x5a, 0x17, 0x82, 0xab, 0xcf, 0xbc, 0x5d,
                  0x18, 0x29, 0xbf, 0x97];
let tag = [0xdb, 0xb7, 0x0d, 0xda, 0xbd, 0xfa, 0x8c, 0xa5,
           0x60, 0xa2, 0x30, 0x3d, 0xe6, 0x07, 0x92, 0x10];

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

try!(decrypt(&key, &nonce, &aad, &ciphertext, &tag, &mut plaintext));

assert_eq!(plaintext, b"hello, world");