Skip to main content

SecretBoxBytes

Struct SecretBoxBytes 

Source
pub struct SecretBoxBytes { /* private fields */ }
Expand description

Fixed-allocation secret bytes with a runtime length.

This type is available with the alloc feature. Unlike SecretVec, its public API cannot grow or shrink the private backing allocation after construction. Mutable exposure receives only &mut [u8], so safe operations cannot reallocate it. The private Vec<u8> representation exists so bounded constructors can use fallible reservation; the safe API never exposes vector growth or ownership extraction.

Replacement requires the same public length. A replacement value is fully constructed in a separate clear-on-drop allocation before the old allocation is cleared and exchanged. Use SecretVec when the secret length must change over time.

Clearing covers the backing allocation’s full capacity, including any allocator-provided spare bytes.

The type deliberately does not implement Clone, Copy, Deref, AsRef<[u8]>, PartialEq, or secret-printing Debug.

Implementations§

Source§

impl SecretBoxBytes

Source

pub fn zeroed(len: usize) -> Self

Allocate len zeroed secret bytes.

len must already be validated as trusted public metadata. Like ordinary infallible Rust allocation APIs, allocation failure may abort the process. Use SecretBoxBytes::try_zeroed for untrusted lengths or availability-sensitive code.

Source

pub fn try_zeroed( len: usize, maximum: usize, ) -> Result<Self, SecretBoxBytesBuildError>

Allocate a bounded fixed-length secret without aborting on reserve failure.

The public maximum is checked before allocation. After try_reserve_exact succeeds, resizing to len cannot allocate.

Source

pub fn from_boxed_slice(inner: Box<[u8]>) -> Self

Take ownership of an existing boxed byte slice.

The allocation is not copied. Its complete length is volatile-cleared when this value is cleared or dropped.

Source

pub fn from_slice(bytes: &[u8]) -> Self

Allocate fixed-length storage and copy bytes into it.

The slice length must already be validated when it comes from untrusted metadata. Use SecretBoxBytes::try_from_slice to apply a public bound and return allocation failure.

Source

pub fn try_from_slice( bytes: &[u8], maximum: usize, ) -> Result<Self, SecretBoxBytesBuildError>

Copy a bounded slice into fallibly allocated fixed-length storage.

Source

pub fn from_fn(len: usize, make_byte: impl FnMut(usize) -> u8) -> Self

Generate each byte directly into fixed-length clear-on-drop storage.

If the generator panics, the partially initialized value is cleared during unwinding. len must already be trusted and bounded; allocation failure may abort. Use SecretBoxBytes::try_from_fn_bounded for untrusted lengths.

Source

pub fn try_from_fn<E>( len: usize, make_byte: impl FnMut(usize) -> Result<u8, E>, ) -> Result<Self, E>

Generate each byte with a fallible generator.

If generation fails, the partially initialized allocation is cleared before the error is returned. This method only reports generator errors; len must already be trusted and bounded, and allocation failure may abort. Use SecretBoxBytes::try_from_fn_bounded when allocation must also be fallible.

Source

pub fn try_from_fn_bounded<E>( len: usize, maximum: usize, make_byte: impl FnMut(usize) -> Result<u8, E>, ) -> Result<Self, SecretBoxBytesGenerateError<E>>

Generate a bounded fixed-length secret with fallible allocation and fallible byte generation.

Source

pub const fn len(&self) -> usize

Number of initialized bytes in the fixed allocation.

Source

pub const fn is_empty(&self) -> bool

Returns true when the fixed allocation has length zero.

Source

pub fn with_secret<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R

Run a closure with direct shared access to the fixed allocation.

The returned value cannot borrow the secret:

use sanitization::SecretBoxBytes;

let secret = SecretBoxBytes::from_slice(b"token");
let escaped = secret.with_secret(|bytes| bytes);
let _ = escaped;
Source

pub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut [u8]) -> R) -> R

Run a closure with direct mutable access to the fixed allocation.

A mutable slice cannot resize or replace the backing allocation.

Source

pub fn copy_to_slice(&self, destination: &mut [u8]) -> Result<(), LengthError>

Copy the secret into a caller-provided slice of the same public length.

Source

pub fn replace_from_slice(&mut self, bytes: &[u8]) -> Result<(), LengthError>

Replace the secret from a same-length slice.

The replacement allocation is constructed before the old allocation is cleared. A length mismatch leaves the existing secret unchanged.

Source

pub fn replace_from_boxed_slice( &mut self, bytes: Box<[u8]>, ) -> Result<(), LengthError>

Replace the secret by taking ownership of a same-length boxed slice.

On length mismatch, the rejected boxed bytes are cleared before this method returns the error.

Source

pub fn replace_from_fn(&mut self, make_byte: impl FnMut(usize) -> u8)

Replace the secret with same-length generated bytes.

The replacement is generated in a fresh clear-on-drop allocation. If generation panics, the old value remains unchanged.

Source

pub fn try_replace_from_fn<E>( &mut self, make_byte: impl FnMut(usize) -> Result<u8, E>, ) -> Result<(), E>

Replace the secret with same-length fallibly generated bytes.

If generation fails, the old value remains unchanged and the partial replacement is cleared before the error is returned.

Source

pub fn clear_secret(&mut self)

Clear every byte while retaining the fixed allocation and length.

Source

pub fn into_cleared(self)

Consume this value after clearing its complete allocation.

Source

pub fn constant_time_eq(&self, other: &[u8]) -> bool

Compare against a slice without early exit for equal-length inputs.

Length is treated as public metadata.

Trait Implementations§

Source§

impl ConstantTimeEq for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn ct_eq(&self, other: &Self) -> Choice

Compare without secret-dependent early exit.
Source§

fn ct_ne(&self, other: &Rhs) -> Choice

Source§

impl ConstantTimeEq for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if two items are equal. Read more
Source§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT equal. Read more
Source§

impl ConstantTimeEq<[u8]> for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn ct_eq(&self, other: &[u8]) -> Choice

Compare without secret-dependent early exit.
Source§

fn ct_ne(&self, other: &Rhs) -> Choice

Source§

impl Debug for SecretBoxBytes

Available on crate feature alloc only.
Source§

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

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

impl Default for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for SecretBoxBytes

Available on crate feature alloc 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 Drop for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl SecureSanitize for SecretBoxBytes

Available on crate feature alloc only.
Source§

fn secure_sanitize(&mut self)

Clear the sensitive bytes owned by this value.
Source§

impl Serialize for SecretBoxBytes

Available on crate feature alloc 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 StableMutableSecretStorage for SecretBoxBytes

Available on crate feature alloc only.
Source§

impl StableSharedSecretStorage for SecretBoxBytes

Available on crate feature alloc only.
Source§

impl Zeroize for SecretBoxBytes

Available on crate feature alloc only.
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 ZeroizeOnDrop for SecretBoxBytes

Available on crate feature alloc only.

Auto Trait Implementations§

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

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