Function private_box::decrypt [] [src]

pub fn decrypt(cyphertext: &[u8], secret_key: &[u8; 32]) -> Option<Vec<u8>>

Attempt to decrypt a private-box message, using your secret key. If you were an intended recipient then the decrypted message is returned as Some(Vec<u8>). If it was not for you, then None will be returned.

Example

extern crate libsodium_sys;
extern crate private_box;

use private_box::{init, encrypt, decrypt};
use libsodium_sys::{
     crypto_box_PUBLICKEYBYTES,
     crypto_box_SECRETKEYBYTES,
     crypto_box_keypair,
};
fn main() {
    let msg : [u8; 3] = [0,1,2];
    let mut alice_pk : [u8; crypto_box_PUBLICKEYBYTES] = [0; crypto_box_PUBLICKEYBYTES]; 
    let mut alice_sk : [u8; crypto_box_SECRETKEYBYTES] = [0; crypto_box_SECRETKEYBYTES]; 
    let mut bob_pk : [u8; crypto_box_PUBLICKEYBYTES] = [0; crypto_box_PUBLICKEYBYTES]; 
    let mut bob_sk : [u8; crypto_box_SECRETKEYBYTES] = [0; crypto_box_SECRETKEYBYTES]; 

    init();
    unsafe {
        crypto_box_keypair(& mut alice_pk, & mut alice_sk);
        crypto_box_keypair(& mut bob_pk, & mut bob_sk);
    }

    let recps: [[u8; 32]; 2] = [alice_pk, bob_pk];
    let cypher = encrypt(&msg, &recps);

    let alice_result = decrypt(&cypher, &alice_sk);
    let bob_result = decrypt(&cypher, &bob_sk);

    assert_eq!(alice_result.unwrap(), msg);
    assert_eq!(bob_result.unwrap(), msg);
}