SecureString

Struct SecureString 

Source
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 and inherits all of its security guarantees.

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

Source

pub fn new() -> Result<Self, Error>

Source

pub fn new_with_capacity(capacity: usize) -> Result<Self, Error>

Source

pub fn erase(&mut self)

Source

pub fn len(&self) -> usize

Returns the length of the inner SecureVec

If you want the character length use char_len

Source

pub fn is_empty(&self) -> bool

Source

pub fn drain(&mut self, range: Range<usize>)

Source

pub fn char_len(&self) -> usize

Source

pub fn push_str(&mut self, string: &str)

Push a &str into the SecureString

Source

pub fn unlock_str<F, R>(&self, f: F) -> R
where F: FnOnce(&str) -> R,

Immutable access as &str

Source

pub fn unlock_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut SecureString) -> R,

Mutable access to the SecureString

Source

pub fn insert_text_at_char_idx( &mut self, char_idx: usize, text_to_insert: &str, ) -> usize

Inserts text at the given character index

§Returns

The number of characters inserted

§Example
use secure_types::SecureString;

let mut string = SecureString::from("GreekFeta");
string.insert_text_at_char_idx(9, "Cheese");
string.unlock_str(|str| {
    assert_eq!(str, "GreekFetaCheese");
});
Source

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

Source§

fn clone(&self) -> SecureString

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<&str> for SecureString

Source§

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<SecureVec<u8>> for SecureString

Source§

fn from(vec: SecureVec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for SecureString

Source§

fn from(s: String) -> SecureString

Creates a new SecureString from a String.

The String is zeroized afterwards.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.