pub fn seal(
    algorithm: EncryptionAlg,
    secret_key: &[u8],
    nonce: &[u8],
    plaintext: &[u8],
) -> Result<Vec<u8>> {
    // AES256GCM is the only encryption algorithm we presently support
    assert_eq!(algorithm, EncryptionAlg::AES256GCM);

    // Nonce must be the expected length
    if nonce.len() != AES256GCM_NONCE_SIZE {
        let msg = format!(
            "nonce must be {} bytes (got {})",
            AES256GCM_NONCE_SIZE,
            nonce.len()
        );
        return Err(ErrorKind::CryptoFailure(msg).into());
    }

    let sealing_key = aead::SealingKey::new(&aead::AES_256_GCM, secret_key)?;

    let tag_len = sealing_key.algorithm().tag_len();
    let mut buffer = Vec::with_capacity(AES256GCM_NONCE_SIZE + plaintext.len() + tag_len);

    buffer.extend_from_slice(nonce);
    buffer.extend_from_slice(plaintext);

    // Add space in the buffer to store the GCM tag
    for _ in 0..tag_len {
        buffer.push(0u8);
    }

    aead::seal_in_place(
        &sealing_key,
        nonce,
        &b""[..],
        &mut buffer[AES256GCM_NONCE_SIZE..],
        tag_len,
    )?;

    Ok(buffer)
}
/// Encrypt the given plaintext using the given algorithm
Encrypt the given plaintext using the given algorithm
Encrypt the given plaintext using the given algorithm
pub fn seal(
    algorithm: EncryptionAlg,
    secret_key: &[u8],
    nonce: &[u8],
    plaintext: &[u8],
) -> Result<Vec<u8>> {
    // AES256GCM is the only encryption algorithm we presently support
    assert_eq!(algorithm, EncryptionAlg::AES256GCM);
    // Nonce must be the expected length
    if nonce.len() != AES256GCM_NONCE_SIZE {
        let msg = format!(
            "nonce must be {} bytes (got {})",
            AES256GCM_NONCE_SIZE,
            nonce.len()
        );
        return Err(ErrorKind::CryptoFailure(msg).into());
    }
    let sealing_key = aead::SealingKey::new(&aead::AES_256_GCM, secret_key)?;
    let tag_len = sealing_key.algorithm().tag_len();
    let mut buffer = Vec::with_capacity(AES256GCM_NONCE_SIZE + plaintext.len() + tag_len);
    buffer.extend_from_slice(nonce);
    buffer.extend_from_slice(plaintext);
    // Add space in the buffer to store the GCM tag
    for _ in 0..tag_len {
        buffer.push(0u8);
    }
    aead::seal_in_place(
        &sealing_key,
        nonce,
        &b""[..],
        &mut buffer[AES256GCM_NONCE_SIZE..],
        tag_len,
    )?;
    Ok(buffer)
}
pub fn seal ( algorithm : EncryptionAlg , secret_key : & [ u8 ] , nonce : & [ u8 ] , plaintext : & [ u8 ] , ) -> Result < Vec < u8 > > { // AES256GCM is the only encryption algorithm we presently support assert_eq ! ( algorithm EncryptionAlg AES256GCM ) ; // Nonce must be the expected length if nonce . len ( ) != AES256GCM_NONCE_SIZE { let msg = format ! ( " " AES256GCM_NONCE_SIZE nonce len ( ) ) ; return Err ( ErrorKind :: CryptoFailure ( msg ) . into ( ) ) ; } let sealing_key = aead :: SealingKey :: new ( & aead :: AES_256_GCM , secret_key ) ? ; let tag_len = sealing_key . algorithm ( ) . tag_len ( ) ; let mut buffer = Vec :: with_capacity ( AES256GCM_NONCE_SIZE + plaintext . len ( ) + tag_len ) ; buffer . extend_from_slice ( nonce ) ; buffer . extend_from_slice ( plaintext ) ; // Add space in the buffer to store the GCM tag for _ in 0 .. tag_len { buffer . push ( 0u8 ) ; } aead :: seal_in_place ( & sealing_key , nonce , & b" " [ .. ] , & mut buffer [ AES256GCM_NONCE_SIZE .. ] , tag_len , ) ? ; Ok ( buffer ) }
Encrypt the given plaintext using the given algorithm
Encrypt the given plaintext using the given algorithm
algorithm
EncryptionAlg
secret_key
&[u8]
nonce
&[u8]
plaintext
&[u8]
algorithm
EncryptionAlg
secret_key
&[u8]
nonce
&[u8]
plaintext
&[u8]