Key

Struct Key 

Source
#[repr(C)]
pub struct Key(pub [u8; 32]);
Expand description

A key used to seal and open an encrypted box.

The underlying memory is zeroed on drop.

Tuple Fields§

§0: [u8; 32]

Implementations§

Source§

impl Key

Source

pub const SIZE: usize = 32usize

The size of a key, in bytes (32).

Source

pub fn from_slice(s: &[u8]) -> Option<Self>

Deserialize a key from a byte slice.

Returns None if the slice length is not 32.

Source

pub fn generate_with_rng<R>(r: &mut R) -> Key
where R: CryptoRng + RngCore,

Generate a new random key using the given cryptographically-secure random number generator.

Source

pub fn generate() -> Key

Generate a new random key.

Source§

impl Key

Source

pub fn seal(&self, msg: &mut [u8], n: &Nonce) -> Hmac

Encrypt a message in place, returning the authentication code.

Source

pub fn open(&self, c: &mut [u8], hmac: &Hmac, n: &Nonce) -> bool

Decrypt an encrypted message in place.

Source

pub fn open_attached_into( &self, input: &[u8], n: &Nonce, out: &mut [u8], ) -> bool

Decrypt an encrypted message with attached authentication code, writing the decrypted message into the provided buffer.

If the decryption fails, out will contain a copy of the encrypted message.

§Panics

Panics if the output buffer length isn’t big enough to hold the plaintext message. The output buffer length should be at least input.len() - Hmac::SIZE.

Source

pub fn seal_attached_into(&self, msg: &[u8], nonce: &Nonce, out: &mut [u8])

Encrypt a message, writing the resulting Hmac and ciphertext into the given output buffer. The output buffer size must be at least msg.len() + Hmac::SIZE.

Trait Implementations§

Source§

impl AsBytes for Key

Source§

fn as_bytes(&self) -> &[u8]

Get the bytes of this value. Read more
Source§

fn as_bytes_mut(&mut self) -> &mut [u8]
where Self: FromBytes,

Get the bytes of this value mutably. Read more
Source§

impl Clone for Key

Source§

fn clone(&self) -> Key

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Zeroize for Key

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

impl FromBytes for Key

Auto Trait Implementations§

§

impl Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnwindSafe for Key

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V