pub struct Secret<T: SecureSanitize> { /* private fields */ }Expand description
Clear-on-drop wrapper for non-byte secret types.
This is useful for structs that implement SecureSanitize by clearing
their sensitive fields. Like SecretBytes, this wrapper intentionally does
not implement Clone, Copy, or secret-printing Debug.
§Clearing Strength
When T = [u8; N], this wrapper clears through SecureSanitize for byte
arrays, which uses the same volatile byte clearing primitive as the rest of
the crate. For fixed-size byte secrets, still prefer SecretBytes<N>,
which avoids implementing Clone, Copy, Deref, AsRef<[u8]>, or
secret-printing Debug.
Scalar values such as u64, arrays of sanitizable values, Option<T>, and
Result<T, E> implement SecureSanitize directly. With alloc, Box<T>,
Vec<T>, and String are supported as well. Construction, drop clearing,
and Secret::into_cleared remain available for all of them.
For byte vectors, prefer SecretVec over Secret<Vec<u8>>. Vec<T> is
supported for generic sanitizable containers and clears the raw allocation
capacity after dropping live elements, but generic exposure is intentionally
unavailable because Vec<T> does not implement the storage-stability
contracts. SecretVec provides reviewed scoped access and rotation that
clears old allocations before release.
Implementations§
Source§impl<T: SecureSanitize> Secret<T>
impl<T: SecureSanitize> Secret<T>
Sourcepub fn into_cleared(self)
pub fn into_cleared(self)
Consume the wrapper after first clearing the wrapped value.
Source§impl<T: StableSharedSecretStorage> Secret<T>
impl<T: StableSharedSecretStorage> Secret<T>
Sourcepub fn with_secret<R>(&self, inspect: impl FnOnce(&T) -> R) -> R
pub fn with_secret<R>(&self, inspect: impl FnOnce(&T) -> R) -> R
Run a closure with shared access to storage certified as shared-stable.
This method is unavailable for types such as Vec<T>, String, and
interior-mutable reallocating containers. Use a dedicated secret
container or implement StableSharedSecretStorage after reviewing
the type’s complete safe shared API.
The returned value cannot borrow the wrapped secret:
use sanitization::Secret;
let secret = Secret::new([1_u8, 2, 3, 4]);
let escaped = secret.with_secret(|bytes| bytes);
let _ = escaped;Source§impl<T: StableMutableSecretStorage> Secret<T>
impl<T: StableMutableSecretStorage> Secret<T>
Sourcepub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut T) -> R) -> R
pub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut T) -> R) -> R
Run a closure with mutable access to storage certified as mutable-stable.
This method is unavailable for generic growable or reallocating
containers. Prefer SecretVec, SecretString, their bounded
variants, or a reviewed fixed-storage type carrying
StableMutableSecretStorage.