pub struct SecretString { /* private fields */ }Expand description
Heap-allocated secret UTF-8 text with clear-on-drop behavior.
This type is available with the alloc feature. Use it for bearer tokens,
passphrases, and textual secrets that must cross APIs as UTF-8. Clearing
uses volatile writes over the full allocation capacity before the internal
byte vector length is set to zero.
Implementations§
Source§impl SecretString
impl SecretString
Sourcepub fn new(inner: String) -> SecretString
pub fn new(inner: String) -> SecretString
Wrap a string using volatile clearing on drop.
Sourcepub fn from_string(text: String) -> SecretString
pub fn from_string(text: String) -> SecretString
Wrap an existing string using volatile clearing on drop.
This is an explicit ownership-taking alias for SecretString::new.
The string allocation is not copied; its full capacity will be
volatile-cleared when this SecretString is cleared or dropped.
Sourcepub fn new_volatile(inner: String) -> SecretString
pub fn new_volatile(inner: String) -> SecretString
Compatibility alias for SecretString::new.
Sourcepub const fn empty() -> SecretString
pub const fn empty() -> SecretString
Create an empty secret string.
Sourcepub fn with_capacity(capacity: usize) -> SecretString
pub fn with_capacity(capacity: usize) -> SecretString
Create an empty secret string with at least the requested byte capacity.
Sourcepub fn with_capacity_volatile(capacity: usize) -> SecretString
pub fn with_capacity_volatile(capacity: usize) -> SecretString
Compatibility alias for SecretString::with_capacity.
Sourcepub fn from_secret_str(text: &str) -> SecretString
pub fn from_secret_str(text: &str) -> SecretString
Create a secret string by copying from a string slice.
Sourcepub fn from_chars(
char_count: usize,
make_char: impl FnMut(usize) -> char,
) -> SecretString
pub fn from_chars( char_count: usize, make_char: impl FnMut(usize) -> char, ) -> SecretString
Create a secret string by generating UTF-8 scalar values directly.
char_count is the number of generated char values, not the final
byte length. Each generated character is encoded into the secret heap
allocation and the small stack encoding buffer is immediately cleared.
Sourcepub fn try_from_chars<E>(
char_count: usize,
make_char: impl FnMut(usize) -> Result<char, E>,
) -> Result<SecretString, E>
pub fn try_from_chars<E>( char_count: usize, make_char: impl FnMut(usize) -> Result<char, E>, ) -> Result<SecretString, E>
Create a secret string by fallibly generating UTF-8 scalar values directly.
If make_char returns an error, any text generated before the error is
cleared before the error is returned.
Sourcepub fn from_secret_str_volatile(text: &str) -> SecretString
pub fn from_secret_str_volatile(text: &str) -> SecretString
Compatibility alias for SecretString::from_secret_str.
Sourcepub fn try_with_secret<R>(
&self,
inspect: impl FnOnce(&str) -> R,
) -> Result<R, Utf8Error>
pub fn try_with_secret<R>( &self, inspect: impl FnOnce(&str) -> R, ) -> Result<R, Utf8Error>
Run a closure with read-only access to the secret text.
The result is fallible because the text is stored internally as bytes to
keep clearing safe without String::as_mut_vec.
Sourcepub fn try_with_secret_mut<R>(
&mut self,
edit: impl FnOnce(&mut str) -> R,
) -> Result<R, Utf8Error>
pub fn try_with_secret_mut<R>( &mut self, edit: impl FnOnce(&mut str) -> R, ) -> Result<R, Utf8Error>
Run a closure with mutable access to the secret text.
The result is fallible because the text is stored internally as bytes to
keep clearing safe without String::as_mut_vec. The closure receives
&mut str, so safe Rust cannot invalidate UTF-8.
Sourcepub fn with_secret_bytes<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R
pub fn with_secret_bytes<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R
Run a closure with read-only access to the secret bytes.
Sourcepub fn push_str(&mut self, text: &str)
pub fn push_str(&mut self, text: &str)
Append text to the secret string.
If capacity must grow, the previous allocation is wiped before it is dropped. Prefer constructing with enough capacity in callers that append repeatedly.
Sourcepub fn replace_from_secret_str(&mut self, text: &str)
pub fn replace_from_secret_str(&mut self, text: &str)
Replace all text with a new string slice.
If capacity must grow, the old allocation is wiped before it is dropped and the old secret bytes are not copied into the replacement allocation.
Sourcepub fn replace_from_string(&mut self, text: String)
pub fn replace_from_string(&mut self, text: String)
Replace all text by taking ownership of an existing string.
The old allocation is cleared before the provided string allocation
becomes the secret storage. The provided string is not copied; its full
capacity will be volatile-cleared when this SecretString is later
cleared or dropped.
Sourcepub fn replace_from_chars(
&mut self,
char_count: usize,
make_char: impl FnMut(usize) -> char,
)
pub fn replace_from_chars( &mut self, char_count: usize, make_char: impl FnMut(usize) -> char, )
Replace all text with generated UTF-8 scalar values.
The replacement text is generated into a fresh clear-on-drop allocation
before the old value is cleared and replaced. If make_char panics, the
old value remains unchanged and partial generated text is cleared during
unwinding.
Sourcepub fn try_replace_from_chars<E>(
&mut self,
char_count: usize,
make_char: impl FnMut(usize) -> Result<char, E>,
) -> Result<(), E>
pub fn try_replace_from_chars<E>( &mut self, char_count: usize, make_char: impl FnMut(usize) -> Result<char, E>, ) -> Result<(), E>
Replace all text with fallibly generated UTF-8 scalar values.
The replacement text is generated into a fresh clear-on-drop allocation
before the old value is cleared and replaced. If make_char returns an
error, the old value remains unchanged and partial generated text is
cleared before the error is returned.
Sourcepub fn clear_secret(&mut self)
pub fn clear_secret(&mut self)
Clear this value immediately with volatile writes.
Sourcepub fn constant_time_eq(&self, other: &str) -> bool
pub fn constant_time_eq(&self, other: &str) -> bool
Compare against UTF-8 text without early exit for equal-length inputs.
Length mismatch returns immediately because the provided string length is treated as public metadata.
The portable fallback is intended to avoid data-dependent early exit, but
it is not a formal hardware-level constant-time guarantee. On x86_64,
enable asm-compare for a stronger compiler boundary. Use a dedicated
constant-time comparison crate if your protocol requires externally
audited timing guarantees.
Sourcepub fn into_cleared(self)
pub fn into_cleared(self)
Consume the wrapper after first clearing the wrapped string.
Trait Implementations§
Source§impl ConstantTimeEq for SecretString
Available on crate feature alloc only.
impl ConstantTimeEq for SecretString
alloc only.Source§impl ConstantTimeEq<str> for SecretString
Available on crate feature alloc only.
impl ConstantTimeEq<str> for SecretString
alloc only.Source§impl Debug for SecretString
Available on crate feature alloc only.
impl Debug for SecretString
alloc only.Source§impl Default for SecretString
Available on crate feature alloc only.
impl Default for SecretString
alloc only.Source§fn default() -> SecretString
fn default() -> SecretString
Source§impl Drop for SecretString
Available on crate feature alloc only.
impl Drop for SecretString
alloc only.Source§impl SecureSanitize for SecretString
Available on crate feature alloc only.
impl SecureSanitize for SecretString
alloc only.