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>
impl<const N: usize> ExpiringSecretBytes<N>
Sourcepub fn from_array(bytes: [u8; N], max_age: Duration) -> Self
pub fn from_array(bytes: [u8; N], max_age: Duration) -> Self
Create an expiring secret from an array, then volatile-clear the input array.
Sourcepub fn from_fn(max_age: Duration, make_byte: impl FnMut(usize) -> u8) -> Self
pub fn from_fn(max_age: Duration, make_byte: impl FnMut(usize) -> u8) -> Self
Create an expiring secret by producing each byte directly.
Sourcepub fn try_from_fn<E>(
max_age: Duration,
make_byte: impl FnMut(usize) -> Result<u8, E>,
) -> Result<Self, E>
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.
Sourcepub fn from_secret(secret: SecretBytes<N>, max_age: Duration) -> Self
pub fn from_secret(secret: SecretBytes<N>, max_age: Duration) -> Self
Wrap an existing SecretBytes<N> and start a new lifetime window.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Returns true when the current secret value has expired.
Sourcepub fn replace_from_slice(&mut self, source: &[u8]) -> Result<(), LengthError>
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.
Sourcepub fn replace_from_array(&mut self, bytes: [u8; N])
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.
Sourcepub fn replace_from_fn(&mut self, make_byte: impl FnMut(usize) -> u8)
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.
Sourcepub fn try_replace_from_fn<E>(
&mut self,
make_byte: impl FnMut(usize) -> Result<u8, E>,
) -> Result<(), E>
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.
Sourcepub fn try_copy_to_slice(
&mut self,
destination: &mut [u8],
) -> Result<(), ExpiringSecretError>
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.
Sourcepub fn try_expose_secret<R>(
&mut self,
inspect: impl FnOnce(&[u8; N]) -> R,
) -> Result<R, SecretExpiredError>
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.
Sourcepub fn try_expose_secret_volatile<R>(
&mut self,
inspect: impl FnOnce(&[u8; N]) -> R,
) -> Result<R, SecretExpiredError>
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.
Sourcepub fn try_constant_time_eq(
&mut self,
other: &[u8],
) -> Result<bool, SecretExpiredError>
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.
Sourcepub fn secure_clear(&mut self)
pub fn secure_clear(&mut self)
Clear the wrapped secret immediately.
Sourcepub fn into_cleared(self)
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.
impl<const N: usize> Debug for ExpiringSecretBytes<N>
std only.Source§impl<const N: usize> Drop for ExpiringSecretBytes<N>
Available on crate feature std only.
impl<const N: usize> Drop for ExpiringSecretBytes<N>
std only.Source§impl<const N: usize> SecureSanitize for ExpiringSecretBytes<N>
Available on crate feature std only.
impl<const N: usize> SecureSanitize for ExpiringSecretBytes<N>
std only.