pub struct SecureString { /* private fields */ }
Expand description
A securely allocated, growable UTF-8 string, just like std::string::String
.
It is a wrapper around SecureVec
Access to the string contents is provided through scoped methods like unlock_str
,
which ensure the memory is only unlocked for the briefest possible time.
§Notes
If you return a new allocated String
from one of the unlock methods you are responsible for zeroizing the memory.
§Example
use secure_types::{SecureString, Zeroize};
// 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.unlock_str(|exposed_str| {
assert_eq!(exposed_str, "my_super_secret_password");
});
// Not recommended but if you allocate a new String make sure to zeroize it
let mut exposed = secret.unlock_str(|exposed_str| {
String::from(exposed_str)
});
// Do what you need to to do with the new string
// When you are done with it, zeroize it
exposed.zeroize();
// 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)
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the inner SecureVec
If you want the character length use char_len
pub fn is_empty(&self) -> bool
pub fn drain(&mut self, range: Range<usize>)
pub fn char_len(&self) -> usize
Sourcepub fn unlock_str<F, R>(&self, f: F) -> R
pub fn unlock_str<F, R>(&self, f: F) -> R
Immutable access as &str
Sourcepub fn unlock_mut<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce(&mut SecureString) -> R,
pub fn unlock_mut<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce(&mut SecureString) -> R,
Mutable access to the SecureString
Sourcepub fn insert_text_at_char_idx(
&mut self,
char_idx: usize,
text_to_insert: &str,
) -> usize
pub fn insert_text_at_char_idx( &mut self, char_idx: usize, text_to_insert: &str, ) -> usize
Sourcepub fn delete_text_char_range(&mut self, char_range: Range<usize>)
pub fn delete_text_char_range(&mut self, char_range: Range<usize>)
Deletes the text in the given character range
§Example
use secure_types::SecureString;
let mut string = SecureString::from("GreekFetaCheese");
string.delete_text_char_range(9..15);
string.unlock_str(|str| {
assert_eq!(str, "GreekFeta");
});
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 moreSource§impl From<&str> for SecureString
impl From<&str> for SecureString
Source§fn from(s: &str) -> SecureString
fn from(s: &str) -> SecureString
Creates a new SecureString
from a &str
.
The &str
is not zeroized, you are responsible for zeroizing it.
Source§impl From<String> for SecureString
impl From<String> for SecureString
Source§fn from(s: String) -> SecureString
fn from(s: String) -> SecureString
Creates a new SecureString
from a String
.
The String
is zeroized afterwards.