Skip to main content

SecretBox

Struct SecretBox 

Source
pub struct SecretBox<S: Zeroize + ?Sized> { /* private fields */ }
Expand description

Heap-allocated secret wrapper — mirrors secrecy::SecretBox.

Stores the secret in a Box<S>, zeroizes on drop, and only exposes the inner value through ExposeSecret / ExposeSecretMut. Debug always prints [REDACTED].

§Migration to native secure-gate

For sized types, convert to Dynamic<S> using the provided From impl:

use secure_gate::compat::v10::SecretBox;
use secure_gate::Dynamic;

let compat: SecretBox<String> = SecretBox::init_with(|| String::from("hunter2"));
let native: Dynamic<String> = compat.into();

Implementations§

Source§

impl<S: Zeroize + ?Sized> SecretBox<S>

Source

pub fn new(boxed_secret: Box<S>) -> Self

Creates a SecretBox from a pre-boxed value.

Source§

impl<S: Zeroize + Default> SecretBox<S>

Source

pub fn init_with_mut(ctr: impl FnOnce(&mut S)) -> Self

Creates a SecretBox by initializing the default value in-place via a mutable closure.

Source§

impl<S: Zeroize + Clone> SecretBox<S>

Source

pub fn init_with(ctr: impl FnOnce() -> S) -> Self

Creates a SecretBox from the return value of ctr.

Makes an effort to zeroize the stack copy before boxing, but this is best-effort. Prefer init_with_mut when possible.

Source

pub fn try_init_with<E>(ctr: impl FnOnce() -> Result<S, E>) -> Result<Self, E>

Fallible variant of init_with.

Trait Implementations§

Source§

impl<S: CloneableSecret> Clone for SecretBox<S>

Source§

fn clone(&self) -> Self

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<S: Zeroize + ?Sized> Debug for SecretBox<S>

Source§

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

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

impl<S: Zeroize + Default> Default for SecretBox<S>

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for SecretBox<T>

Available on crate feature serde-deserialize only.
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<S: Zeroize + ?Sized> Drop for SecretBox<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<S: Zeroize + ?Sized> ExposeSecret<S> for SecretBox<S>

Source§

fn expose_secret(&self) -> &S

Returns a shared reference to the inner secret.
Source§

impl<S: Zeroize + ?Sized> ExposeSecretMut<S> for SecretBox<S>

Source§

fn expose_secret_mut(&mut self) -> &mut S

Returns a mutable reference to the inner secret.
Source§

impl<S: Zeroize + ?Sized> From<Box<S>> for SecretBox<S>

Source§

fn from(source: Box<S>) -> Self

Converts to this type from the input type.
Source§

impl From<Dynamic<String>> for SecretBox<String>

Converts a Dynamic<String> back into a SecretBox<String>.

Clones the inner String. Both the source and the new wrapper are zeroized on drop.

Source§

fn from(d: Dynamic<String>) -> Self

Converts to this type from the input type.
Source§

impl<S: Clone + Zeroize + 'static> From<Dynamic<Vec<S>>> for SecretBox<Vec<S>>

Converts a Dynamic<Vec<S>> back into a SecretBox<Vec<S>>.

Clones the inner Vec. Both ends are zeroized on drop.

Source§

fn from(d: Dynamic<Vec<S>>) -> Self

Converts to this type from the input type.
Source§

impl<S: Clone + Zeroize + 'static> From<SecretBox<S>> for Dynamic<S>

Converts a SecretBox<S> into a Dynamic<S> (primary migration path).

Requires S: Clone because the inner Box<S> cannot be moved out of SecretBox without unsafe code (SecretBox has a Drop impl). The clone is immediately wrapped in Dynamic and the original is zeroized on drop.

For zero-copy migration, construct Dynamic<S> directly instead.

Source§

fn from(sb: SecretBox<S>) -> Self

Converts to this type from the input type.
Source§

impl From<SecretBox<str>> for Dynamic<String>

Converts a SecretString (= SecretBox<str>) into a Dynamic<String>.

Clones the inner str into a new String. Both ends are zeroized on drop.

Source§

fn from(sb: SecretString) -> Self

Converts to this type from the input type.
Source§

impl<T> Serialize for SecretBox<T>

Available on crate feature serde-serialize only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<S: Zeroize + ?Sized> Zeroize for SecretBox<S>

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<S: Zeroize + ?Sized> ZeroizeOnDrop for SecretBox<S>

Auto Trait Implementations§

§

impl<S> Freeze for SecretBox<S>
where S: ?Sized,

§

impl<S> RefUnwindSafe for SecretBox<S>
where S: RefUnwindSafe + ?Sized,

§

impl<S> Send for SecretBox<S>
where S: Send + ?Sized,

§

impl<S> Sync for SecretBox<S>
where S: Sync + ?Sized,

§

impl<S> Unpin for SecretBox<S>
where S: ?Sized,

§

impl<S> UnsafeUnpin for SecretBox<S>
where S: ?Sized,

§

impl<S> UnwindSafe for SecretBox<S>
where S: UnwindSafe + ?Sized,

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