pub struct Hidden<T>
where T: Zeroize,
{ /* private fields */ }
Expand description

A generic type for data that needs to be kept hidden and zeroized when out of scope, and is accessible only by reference.

You can define a hidden type using any underlying data type that implements Zeroize. This is the case for most basic types that you probably care about.

Hidden data has useful properties:

  • The data is not subject to Debug or Display output, which are masked.
  • The data can only be accessed by (immutable or mutable) reference.
  • The data zeroizes when dropped, and can also be manually zeroized.
  • Cloning is safe.

Note that it may not be safe to dereference the hidden data if its type implements Copy. If the type does not implement Copy, you should be fine. If it does, avoid dereferencing.

Hidden data supports transparent deserialization, but you’ll need to implement serialization yourself if you need it.


// In this example, we need to handle secret data of type `[u8; 32]`.

// We can create hidden data from existing data; in this case, it's the caller's responsibility to make sure the existing data is handled securely
let hidden_from_data = Hidden::<[u8; 32]>::hide([1u8; 32]);

// We can access the hidden data as a reference, but not take ownership of it
assert_eq!(hidden_from_data.reveal(), &[1u8; 32]);

// We can create default hidden data and then modify it as a mutable reference; this is common for functions that act on data in place
let mut hidden_in_place = Hidden::<[u8; 32]>::hide([0u8; 32]);
let hidden_in_place_mut_ref = hidden_in_place.reveal_mut();
*hidden_in_place_mut_ref = [42u8; 32];
assert_eq!(hidden_in_place.reveal(), &[42u8; 32]);

// Cloning is safe to do
let mut clone = hidden_in_place.clone();
assert_eq!(hidden_in_place.reveal(), clone.reveal());

// You can manually zeroize the data if you need to
clone.zeroize();
assert_eq!(clone.reveal(), &[0u8; 32]);

Implementations§

source§

impl<T> Hidden<T>
where T: Zeroize,

source

pub fn hide(inner: T) -> Self

Create new hidden data from the underlying type

source

pub fn reveal(&self) -> &T

Reveal the hidden data as an immutable reference

source

pub fn reveal_mut(&mut self) -> &mut T

Reveal the hidden data as a mutable reference

Trait Implementations§

source§

impl<T> Clone for Hidden<T>
where T: Zeroize + Clone,

source§

fn clone(&self) -> Hidden<T>

Returns a copy 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<T> Debug for Hidden<T>
where T: Zeroize,

Only output masked data for debugging, keeping the hidden data hidden

source§

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

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

impl<'de, T> Deserialize<'de> for Hidden<T>
where T: Zeroize + Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> Display for Hidden<T>
where T: Zeroize,

Only display masked data, keeping the hidden data hidden

source§

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

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

impl<T> Drop for Hidden<T>
where T: Zeroize,

Zeroize the hidden data when dropped

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Zeroize for Hidden<T>
where T: Zeroize,

Zeroize the hidden data

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.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Hidden<T>
where T: RefUnwindSafe,

§

impl<T> Send for Hidden<T>
where T: Send,

§

impl<T> Sync for Hidden<T>
where T: Sync,

§

impl<T> Unpin for Hidden<T>

§

impl<T> UnwindSafe for Hidden<T>
where T: UnwindSafe,

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> 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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,