Skip to main content

SecretString

Struct SecretString 

Source
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

Source

pub fn new(inner: String) -> SecretString

Wrap a string using volatile clearing on drop.

Source

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.

Source

pub fn new_volatile(inner: String) -> SecretString

Compatibility alias for SecretString::new.

Source

pub const fn empty() -> SecretString

Create an empty secret string.

Source

pub fn with_capacity(capacity: usize) -> SecretString

Create an empty secret string with at least the requested byte capacity.

Source

pub fn with_capacity_volatile(capacity: usize) -> SecretString

Compatibility alias for SecretString::with_capacity.

Source

pub fn from_secret_str(text: &str) -> SecretString

Create a secret string by copying from a string slice.

Source

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.

Source

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.

Source

pub fn from_secret_str_volatile(text: &str) -> SecretString

Compatibility alias for SecretString::from_secret_str.

Source

pub fn len(&self) -> usize

Number of bytes currently held.

Source

pub fn is_empty(&self) -> bool

Returns true when no text is held.

Source

pub fn capacity(&self) -> usize

Current allocation capacity in bytes.

Source

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.

Source

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.

Source

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

Run a closure with read-only access to the secret bytes.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn clear_secret(&mut self)

Clear this value immediately with volatile writes.

Source

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.

Source

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.
Source§

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

Compare without secret-dependent early exit.
Source§

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

Source§

impl ConstantTimeEq<str> for SecretString

Available on crate feature alloc only.
Source§

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

Compare without secret-dependent early exit.
Source§

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

Source§

impl Debug for SecretString

Available on crate feature alloc only.
Source§

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

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

impl Default for SecretString

Available on crate feature alloc only.
Source§

fn default() -> SecretString

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

impl Drop for SecretString

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 SecretString

Available on crate feature alloc only.
Source§

fn secure_sanitize(&mut self)

Clear the sensitive bytes owned by this value.

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