pub struct SecretString { /* private fields */ }Expand description
UTF-8 secret stored in guarded, zeroizing memory.
The value intentionally implements neither Display nor direct string
access. Use SecureString::with_str or SecureString::with_bytes and do
not retain copies beyond the callback.
Implementations§
Source§impl SecureString
impl SecureString
Sourcepub fn new() -> SecureString
pub fn new() -> SecureString
Creates an empty secure string without allocating secret pages.
Sourcepub fn try_from_bytes(bytes: Vec<u8>) -> Result<SecureString, Error>
pub fn try_from_bytes(bytes: Vec<u8>) -> Result<SecureString, Error>
Moves bytes into secure memory and zeroizes the input vector.
UTF-8 is validated when text access is requested. Prefer
SecureString::try_from_utf8 when invalid input should fail eagerly.
Sourcepub fn try_from_slice(bytes: &[u8]) -> Result<SecureString, Error>
pub fn try_from_slice(bytes: &[u8]) -> Result<SecureString, Error>
Copies bytes into a new secure string.
Sourcepub fn from_secure_vec(bytes: SecureVec) -> SecureString
pub fn from_secure_vec(bytes: SecureVec) -> SecureString
Wraps an existing secure byte vector without copying it.
Sourcepub fn try_from_utf8(bytes: Vec<u8>) -> Result<SecureString, Error>
pub fn try_from_utf8(bytes: Vec<u8>) -> Result<SecureString, Error>
Validates UTF-8, moves the bytes into secure memory, and zeroizes input.
Sourcepub fn try_from_env(name: &str) -> Result<Option<SecureString>, Error>
pub fn try_from_env(name: &str) -> Result<Option<SecureString>, Error>
Reads an environment variable directly into a secure string when set.
Sourcepub fn try_from_os_string(value: OsString) -> Result<SecureString, Error>
pub fn try_from_os_string(value: OsString) -> Result<SecureString, Error>
Converts an operating-system string into a secure UTF-8 representation.
Sourcepub fn try_clone(&self) -> Result<SecureString, Error>
pub fn try_clone(&self) -> Result<SecureString, Error>
Creates an independent secure copy of this string.
Sourcepub fn with_str<R>(&self, f: impl FnOnce(&str) -> R) -> Result<R, Error>
pub fn with_str<R>(&self, f: impl FnOnce(&str) -> R) -> Result<R, Error>
Exposes valid UTF-8 text only for the duration of f.
Sourcepub fn with_str_in<R>(
&self,
access: &SecureReadAccess<'_>,
f: impl FnOnce(&str) -> R,
) -> Result<R, Error>
pub fn with_str_in<R>( &self, access: &SecureReadAccess<'_>, f: impl FnOnce(&str) -> R, ) -> Result<R, Error>
Exposes valid UTF-8 text using an existing secure read scope.
Sourcepub fn with_bytes<R>(&self, f: impl FnOnce(&[u8]) -> R) -> Result<R, Error>
pub fn with_bytes<R>(&self, f: impl FnOnce(&[u8]) -> R) -> Result<R, Error>
Exposes the encoded bytes only for the duration of f.
Sourcepub fn with_bytes_in<R>(
&self,
access: &SecureReadAccess<'_>,
f: impl FnOnce(&[u8]) -> R,
) -> Result<R, Error>
pub fn with_bytes_in<R>( &self, access: &SecureReadAccess<'_>, f: impl FnOnce(&[u8]) -> R, ) -> Result<R, Error>
Exposes encoded bytes using an existing secure read scope.
Sourcepub fn append_to_secure_vec(&self, target: &mut SecureVec) -> Result<(), Error>
pub fn append_to_secure_vec(&self, target: &mut SecureVec) -> Result<(), Error>
Appends this value to target without copying through normal heap memory.
Sourcepub fn try_push_byte(&mut self, byte: u8) -> Result<(), Error>
pub fn try_push_byte(&mut self, byte: u8) -> Result<(), Error>
Appends one byte to the secure string.
This low-level operation does not validate that the resulting value is UTF-8; subsequent text access will fail if the sequence is invalid.
Sourcepub fn try_extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), Error>
pub fn try_extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), Error>
Appends bytes without intermediate normal-memory allocation.
Sourcepub fn try_push_utf8_char(&mut self, ch: char) -> Result<(), Error>
pub fn try_push_utf8_char(&mut self, ch: char) -> Result<(), Error>
Encodes and appends one Unicode scalar value.