Skip to main content

ExpiringSecretBytes

Struct ExpiringSecretBytes 

Source
pub struct ExpiringSecretBytes<const N: usize> { /* private fields */ }
Expand description

Fixed-size secret bytes with std lifetime enforcement.

This type is available with the std feature. It wraps SecretBytes<N>, tracks creation time with std::time::Instant, and rejects exposure after the configured maximum age. On expiration, fallible read/exposure/comparison methods clear the wrapped secret before returning SecretExpiredError.

There is no background task. Expiration is checked only when a method is called.

Implementations§

Source§

impl<const N: usize> ExpiringSecretBytes<N>

Source

pub fn zeroed(max_age: Duration) -> Self

Create an all-zero expiring secret.

Source

pub fn from_array(bytes: [u8; N], max_age: Duration) -> Self

Create an expiring secret from an array, then volatile-clear the input array.

Source

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

Create an expiring secret by producing each byte directly.

Source

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

Create an expiring secret by fallibly producing each byte directly.

If make_byte returns an error, any bytes generated before the error are cleared before the error is returned.

Source

pub fn from_secret(secret: SecretBytes<N>, max_age: Duration) -> Self

Wrap an existing SecretBytes<N> and start a new lifetime window.

Source

pub const fn len(&self) -> usize

Number of bytes stored in this secret.

Source

pub const fn is_empty(&self) -> bool

Returns true when the secret has zero length.

Source

pub const fn max_age(&self) -> Duration

Configured maximum age for the current secret value.

Source

pub fn age(&self) -> Duration

Elapsed lifetime of the current secret value.

Source

pub fn is_expired(&self) -> bool

Returns true when the current secret value has expired.

Source

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

Replace all bytes and restart the lifetime window.

The replacement is validated and staged first. The old value is then volatile-cleared before the replacement is installed.

Source

pub fn replace_from_array(&mut self, bytes: [u8; N])

Replace all bytes from an owned array, clear that input array, and restart the lifetime window.

The replacement is staged first. The old value is then volatile-cleared before the replacement is installed.

Source

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

Replace all bytes from a generator and restart the lifetime window.

If the previous value has already expired, it is cleared before the new value is generated. If make_byte panics and the old value was still live, 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 all bytes from a fallible generator and restart the lifetime window.

If the old value is still live and generation fails, the old value remains unchanged. If the old value has already expired, it is cleared before generation and remains cleared if generation fails.

Source

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

Fill a caller-provided destination with a copy of the secret bytes if the secret has not expired.

Source

pub fn try_expose_secret<R>( &mut self, inspect: impl FnOnce(&[u8; N]) -> R, ) -> Result<R, SecretExpiredError>

Run a closure with a temporary array copy if the secret has not expired.

Source

pub fn try_expose_secret_volatile<R>( &mut self, inspect: impl FnOnce(&[u8; N]) -> R, ) -> Result<R, SecretExpiredError>

Run a closure with a temporary array copy if the secret has not expired.

This is the expiring variant of SecretBytes::expose_secret_volatile.

Source

pub fn try_constant_time_eq( &mut self, other: &[u8], ) -> Result<bool, SecretExpiredError>

Compare against a slice if the secret has not expired.

Length mismatch remains public metadata and returns Ok(false).

This delegates to SecretBytes::constant_time_eq; see that method for portable fallback timing limits.

Source

pub fn secure_clear(&mut self)

Clear the wrapped secret immediately.

Source

pub fn into_cleared(self)

Consume this value after first clearing the wrapped secret.

Drop still runs after this method returns, so the wrapped storage is cleared a second time on the normal path.

Trait Implementations§

Source§

impl<const N: usize> Debug for ExpiringSecretBytes<N>

Available on crate feature std only.
Source§

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

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

impl<const N: usize> Drop for ExpiringSecretBytes<N>

Available on crate feature std 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<const N: usize> SecureSanitize for ExpiringSecretBytes<N>

Available on crate feature std only.
Source§

fn secure_sanitize(&mut self)

Clear the sensitive bytes owned by this value.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for ExpiringSecretBytes<N>

§

impl<const N: usize> RefUnwindSafe for ExpiringSecretBytes<N>

§

impl<const N: usize> Send for ExpiringSecretBytes<N>

§

impl<const N: usize> Sync for ExpiringSecretBytes<N>

§

impl<const N: usize> Unpin for ExpiringSecretBytes<N>

§

impl<const N: usize> UnsafeUnpin for ExpiringSecretBytes<N>

§

impl<const N: usize> UnwindSafe for ExpiringSecretBytes<N>

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