pub struct SecureString { /* private fields */ }
Expand description
A securely allocated, growable UTF-8 string, analogous to std::string::String
.
It is a wrapper around SecureVec<u8>
and inherits all of its security guarantees,
ensuring that sensitive string data like passwords, API keys, or personal information
is handled with care.
§Security Model
SecureString
enforces the same security model as SecureVec
:
- Zeroization on Drop: The string’s buffer is securely wiped clean.
- Memory Locking & Encryption: When the
std
feature is enabled, the buffer is protected against OS-level spying via disk swaps or memory inspection tools.
Access to the string contents is provided through scoped methods like str_scope
,
which ensure the memory is only unlocked for the briefest possible time.
§Security Considerations
While the crate protects the memory, you must still be careful not to leak the data.
For example, creating a new, unsecured String
from the unlocked slice and returning
it from the scope would leak the sensitive data if not handled correctly.
§Examples
use secure_types::SecureString;
// Create a SecureString
let mut secret = SecureString::from("my_super_secret");
// The memory is locked here
// Safely append more data.
secret.push_str("_password");
// The memory is locked here.
// Use a scope to safely access the content as a &str.
secret.str_scope(|exposed_str| {
assert_eq!(exposed_str, "my_super_secret_password");
});
// When `secret` is dropped, its data zeroized.
Implementations§
Source§impl SecureString
impl SecureString
pub fn new() -> Result<Self, Error>
pub fn new_with_capacity(capacity: usize) -> Result<Self, Error>
pub fn erase(&mut self)
pub fn len(&self) -> usize
pub fn drain(&mut self, range: Range<usize>)
pub fn char_len(&self) -> usize
Sourcepub fn str_scope<F, R>(&self, f: F) -> R
pub fn str_scope<F, R>(&self, f: F) -> R
Access the SecureString
as &str
§Use with caution
You can actually return a new allocated String
from this function
If you do that you are responsible for zeroizing its contents
Sourcepub fn mut_scope<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce(&mut SecureString) -> R,
pub fn mut_scope<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce(&mut SecureString) -> R,
Mutable access to the SecureString
§Use with caution
You can actually return a new allocated String
from this function
If you do that you are responsible for zeroizing its contents
pub fn insert_text_at_char_idx( &mut self, char_idx: usize, text_to_insert: &str, ) -> usize
pub fn delete_text_char_range(&mut self, char_range: Range<usize>)
Trait Implementations§
Source§impl Clone for SecureString
impl Clone for SecureString
Source§fn clone(&self) -> SecureString
fn clone(&self) -> SecureString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more