pub fn write<O: Write>(plain: &[u8], output: &mut O, key: &Key, associated_data: &[u8]) -> crate::Result<()> {
    // write magic and version bytes
    output.write_all(&MAGIC)?;
    output.write_all(&VERSION)?;

    // create ephemeral key pair.
    let ephemeral_key = x25519::SecretKey::generate()?;

    // get public key.
    let ephemeral_pk = ephemeral_key.public_key();

    let ephemeral_pk_bytes = ephemeral_pk.to_bytes();

    // write public key into output.
    output.write_all(&ephemeral_pk_bytes)?;

    // secret key now expects an array
    let mut key_bytes = [0u8; x25519::SECRET_KEY_LENGTH];
    key_bytes.clone_from_slice(key);

    // get `x25519` secret key from public key.
    let pk = x25519::SecretKey::from_bytes(key_bytes).public_key();

    let pk_bytes = pk.to_bytes();

    // do a diffie_hellman exchange to make a shared secret key.
    let shared = ephemeral_key.diffie_hellman(&pk);

    // compute the nonce using the ephemeral keys.
    let nonce = {
        let mut i = ephemeral_pk.to_bytes().to_vec();
        i.extend_from_slice(&pk_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 the XChaCha20Poly1305 tag.
    let mut tag = [0; XChaCha20Poly1305::TAG_LENGTH];

    // creates the ciphertext.
    let mut ct = vec![0; plain.len()];
    XChaCha20Poly1305::try_encrypt(&shared.to_bytes(), &nonce, associated_data, plain, &mut ct, &mut tag)?;

    // write tag and ciphertext into the output.
    output.write_all(&tag)?;
    output.write_all(&ct)?;

    Ok(())
}
/// Encrypt the opaque plaintext bytestring using the specified [`Key`] and optional associated data
/// and writes the ciphertext to the specifed output
Encrypt the opaque plaintext bytestring using the specified [`Key`] and optional associated data
and writes the ciphertext to the specifed output
Encrypt the opaque plaintext bytestring using the specified [ ` Key ` ] and optional associated data and writes the ciphertext to the specifed output
pub fn write<O: Write>(plain: &[u8], output: &mut O, key: &Key, associated_data: &[u8]) -> crate::Result<()> {
    // write magic and version bytes
    output.write_all(&MAGIC)?;
    output.write_all(&VERSION)?;
    // create ephemeral key pair.
    let ephemeral_key = x25519::SecretKey::generate()?;
    // get public key.
    let ephemeral_pk = ephemeral_key.public_key();
    let ephemeral_pk_bytes = ephemeral_pk.to_bytes();
    // write public key into output.
    output.write_all(&ephemeral_pk_bytes)?;
    // secret key now expects an array
    let mut key_bytes = [0u8; x25519::SECRET_KEY_LENGTH];
    key_bytes.clone_from_slice(key);
    // get `x25519` secret key from public key.
    let pk = x25519::SecretKey::from_bytes(key_bytes).public_key();
    let pk_bytes = pk.to_bytes();
    // do a diffie_hellman exchange to make a shared secret key.
    let shared = ephemeral_key.diffie_hellman(&pk);
    // compute the nonce using the ephemeral keys.
    let nonce = {
        let mut i = ephemeral_pk.to_bytes().to_vec();
        i.extend_from_slice(&pk_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 the XChaCha20Poly1305 tag.
    let mut tag = [0; XChaCha20Poly1305::TAG_LENGTH];
    // creates the ciphertext.
    let mut ct = vec![0; plain.len()];
    XChaCha20Poly1305::try_encrypt(&shared.to_bytes(), &nonce, associated_data, plain, &mut ct, &mut tag)?;
    // write tag and ciphertext into the output.
    output.write_all(&tag)?;
    output.write_all(&ct)?;
    Ok(())
}
pub fn write < O : Write > ( plain : & [ u8 ] , output : & mut O , key : & Key , associated_data : & [ u8 ] ) -> crate :: Result < ( ) > { // write magic and version bytes output . write_all ( & MAGIC ) ? ; output . write_all ( & VERSION ) ? ; // create ephemeral key pair. let ephemeral_key = x25519 :: SecretKey :: generate ( ) ? ; // get public key. let ephemeral_pk = ephemeral_key . public_key ( ) ; let ephemeral_pk_bytes = ephemeral_pk . to_bytes ( ) ; // write public key into output. output . write_all ( & ephemeral_pk_bytes ) ? ; // secret key now expects an array let mut key_bytes = [ 0u8 ; x25519 :: SECRET_KEY_LENGTH ] ; key_bytes . clone_from_slice ( key ) ; // get `x25519` secret key from public key. let pk = x25519 :: SecretKey :: from_bytes ( key_bytes ) . public_key ( ) ; let pk_bytes = pk . to_bytes ( ) ; // do a diffie_hellman exchange to make a shared secret key. let shared = ephemeral_key . diffie_hellman ( & pk ) ; // compute the nonce using the ephemeral keys. let nonce = { let mut i = ephemeral_pk . to_bytes ( ) . to_vec ( ) ; i . extend_from_slice ( & pk_bytes ) ; let res = blake2b :: Blake2b256 :: digest ( & i ) . to_vec ( ) ; let v : Nonce = res [ 0 .. NONCE_SIZE ] . try_into ( ) . expect ( " " ) ; v } ; // create the XChaCha20Poly1305 tag. let mut tag = [ 0 ; XChaCha20Poly1305 :: TAG_LENGTH ] ; // creates the ciphertext. let mut ct = vec ! [ 0 plain len ( ) ] ; XChaCha20Poly1305 :: try_encrypt ( & shared . to_bytes ( ) , & nonce , associated_data , plain , & mut ct , & mut tag ) ? ; // write tag and ciphertext into the output. output . write_all ( & tag ) ? ; output . write_all ( & ct ) ? ; Ok ( ( ) ) }
Encrypt the opaque plaintext bytestring using the specified [`Key`] and optional associated data
and writes the ciphertext to the specifed output
Encrypt the opaque plaintext bytestring using the specified [ ` Key ` ] and optional associated data and writes the ciphertext to the specifed output
plain
&[u8]
output
&mut O
key
&Key
associated_data
&[u8]
plain
&[u8]
output
&mut O
key
&Key
associated_data
&[u8]