/// Read ciphertext from the input, decrypts it using the specified key and the associated data
/// specified during encryption and returns the plaintext
Read ciphertext from the input, decrypts it using the specified key and the associated data
specified during encryption and returns the plaintext
Read ciphertext from the input decrypts it using the specified key and the associated data specified during encryption and returns the plaintext
pub fn read<I: Read>(input: &mut I, key: &Key, associated_data: &[u8]) -> crate::Result<Vec<u8>> {
    // check the header for structure.
    check_header(input)?;
    // create ephemeral private key.
    let mut ephemeral_pk = [0; x25519::PUBLIC_KEY_LENGTH];
    // get ephemeral private key from input.
    input.read_exact(&mut ephemeral_pk)?;
    // creating the secret key now expects an array
    let mut key_bytes = [0u8; x25519::SECRET_KEY_LENGTH];
    key_bytes.clone_from_slice(key);
    // derive public key from ephemeral private key
    let ephemeral_pk = x25519::PublicKey::from_bytes(ephemeral_pk);
    // get x25519 key pair from ephemeral private key.
    let sk = x25519::SecretKey::from_bytes(key_bytes);
    let pk = sk.public_key();
    // diffie hellman to create the shared secret.
    let shared = sk.diffie_hellman(&ephemeral_pk);
    // compute the nonce using the ephemeral keys.
    let nonce = {
        let mut i = ephemeral_pk.to_bytes().to_vec();
        i.extend_from_slice(&pk.to_bytes());
        let res = blake2b::Blake2b256::digest(&i).to_vec();
        let v: Nonce = res[0..NONCE_SIZE].try_into().expect("slice with incorrect length");
        v
    };
    // create and read tag from input.
    let mut tag = [0; XChaCha20Poly1305::TAG_LENGTH];
    input.read_exact(&mut tag)?;
    // create and read ciphertext from input.
    let mut ct = Vec::new();
    input.read_to_end(&mut ct)?;
    // create plain text buffer.
    let mut pt = vec![0; ct.len()];
    // attempt to decrypt the ciphertext into the plain text buffer.
    XChaCha20Poly1305::try_decrypt(&shared.to_bytes(), &nonce, associated_data, &mut pt, &ct, &tag)?;
    Ok(pt)
}
pub fn read < I : Read > ( input : & mut I , key : & Key , associated_data : & [ u8 ] ) -> crate :: Result < Vec < u8 > > { // check the header for structure. check_header ( input ) ? ; // create ephemeral private key. let mut ephemeral_pk = [ 0 ; x25519 :: PUBLIC_KEY_LENGTH ] ; // get ephemeral private key from input. input . read_exact ( & mut ephemeral_pk ) ? ; // creating the secret key now expects an array let mut key_bytes = [ 0u8 ; x25519 :: SECRET_KEY_LENGTH ] ; key_bytes . clone_from_slice ( key ) ; // derive public key from ephemeral private key let ephemeral_pk = x25519 :: PublicKey :: from_bytes ( ephemeral_pk ) ; // get x25519 key pair from ephemeral private key. let sk = x25519 :: SecretKey :: from_bytes ( key_bytes ) ; let pk = sk . public_key ( ) ; // diffie hellman to create the shared secret. let shared = sk . diffie_hellman ( & ephemeral_pk ) ; // compute the nonce using the ephemeral keys. let nonce = { let mut i = ephemeral_pk . to_bytes ( ) . to_vec ( ) ; i . extend_from_slice ( & pk . to_bytes ( ) ) ; let res = blake2b :: Blake2b256 :: digest ( & i ) . to_vec ( ) ; let v : Nonce = res [ 0 .. NONCE_SIZE ] . try_into ( ) . expect ( " " ) ; v } ; // create and read tag from input. let mut tag = [ 0 ; XChaCha20Poly1305 :: TAG_LENGTH ] ; input . read_exact ( & mut tag ) ? ; // create and read ciphertext from input. let mut ct = Vec :: new ( ) ; input . read_to_end ( & mut ct ) ? ; // create plain text buffer. let mut pt = vec ! [ 0 ct len ( ) ] ; // attempt to decrypt the ciphertext into the plain text buffer. XChaCha20Poly1305 :: try_decrypt ( & shared . to_bytes ( ) , & nonce , associated_data , & mut pt , & ct , & tag ) ? ; Ok ( pt ) }
Read ciphertext from the input, decrypts it using the specified key and the associated data
specified during encryption and returns the plaintext
Read ciphertext from the input decrypts it using the specified key and the associated data specified during encryption and returns the plaintext
input
&mut I
key
&Key
associated_data
&[u8]