Skip to main content

Crypt

Struct Crypt 

Source
pub struct Crypt { /* private fields */ }
Available on crate features aead-chacha20 or aead-aes-gcm only.
Expand description

High-level encryption handle.

Crypt is cheap to construct and to clone — it carries only the algorithm choice, not any key material. Keys are passed per-call to encrypt and decrypt, and never stored inside Crypt itself.

§Defaults

Crypt::new() returns a handle configured for Algorithm::ChaCha20Poly1305. Use Crypt::with_algorithm to pick a different algorithm.

Implementations§

Source§

impl Crypt

Source

pub const fn new() -> Self

Construct a Crypt with the default algorithm (Algorithm::ChaCha20Poly1305).

Source

pub const fn with_algorithm(algorithm: Algorithm) -> Self

Construct a Crypt with an explicit algorithm.

Source

pub const fn aes_256_gcm() -> Self

Available on crate feature aead-aes-gcm only.

Convenience constructor for Algorithm::Aes256Gcm. Available only when the aead-aes-gcm Cargo feature is enabled.

Equivalent to Crypt::with_algorithm(Algorithm::Aes256Gcm). Provided because picking AES-GCM is an explicit, deliberate choice — usually driven by an interop requirement or by a target platform with AES-NI / ARMv8 crypto extensions — and the call site reads cleaner when it says so.

Source

pub const fn algorithm(&self) -> Algorithm

The algorithm this handle is configured to use.

Source

pub fn encrypt(&self, key: &[u8], plaintext: &[u8]) -> Result<Vec<u8>>

Encrypt plaintext under key and return nonce || ciphertext || tag.

A fresh 12-byte nonce is generated for every call via OS-backed CSPRNG (mod_rand::tier3::fill_bytes). The nonce is prepended to the returned buffer so the corresponding decrypt call needs only the key and the buffer.

§Errors
§Example
use crypt_io::Crypt;
let crypt = Crypt::new();
let key = [0u8; 32];
let ciphertext = crypt.encrypt(&key, b"hello").expect("encrypt");
assert!(ciphertext.len() > 5);
Source

pub fn encrypt_with_aad( &self, key: &[u8], plaintext: &[u8], aad: &[u8], ) -> Result<Vec<u8>>

Encrypt plaintext under key with additional authenticated data.

aad is authenticated alongside the ciphertext but is not encrypted and is not included in the returned buffer. Callers must supply identical aad to decrypt_with_aad — otherwise authentication will fail.

Pass &[] for aad to encrypt without associated data, or call the convenience method encrypt which does so.

§Errors

Same as encrypt.

Source

pub fn decrypt(&self, key: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>>

Decrypt a buffer produced by encrypt and return the plaintext.

The buffer is expected to be nonce || ciphertext || tag — exactly the layout encrypt returns. The tag is verified in constant time; any tampering, wrong key, or wrong length results in Error::AuthenticationFailed.

The returned Vec<u8> does not auto-zeroize. Callers handling long-lived keys should move the bytes into a Zeroizing<Vec<u8>> (zeroize crate) or — for production use cases — keep the plaintext in a key-vault handle and never let it touch a raw Vec.

§Errors
§Example
use crypt_io::Crypt;
let crypt = Crypt::new();
let key = [0u8; 32];
let ciphertext = crypt.encrypt(&key, b"hello").expect("encrypt");
let recovered = crypt.decrypt(&key, &ciphertext).expect("decrypt");
assert_eq!(&*recovered, b"hello");
Source

pub fn decrypt_with_aad( &self, key: &[u8], ciphertext: &[u8], aad: &[u8], ) -> Result<Vec<u8>>

Decrypt with associated data. aad must match what was passed to encrypt_with_aad.

§Errors

Same as decrypt.

Trait Implementations§

Source§

impl Clone for Crypt

Source§

fn clone(&self) -> Crypt

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Crypt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Crypt

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Crypt

Source§

fn eq(&self, other: &Crypt) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Crypt

Source§

impl Eq for Crypt

Source§

impl StructuralPartialEq for Crypt

Auto Trait Implementations§

§

impl Freeze for Crypt

§

impl RefUnwindSafe for Crypt

§

impl Send for Crypt

§

impl Sync for Crypt

§

impl Unpin for Crypt

§

impl UnsafeUnpin for Crypt

§

impl UnwindSafe for Crypt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.