Struct secrets::Secret [] [src]

pub struct Secret<T> {
    // some fields omitted
}

A type that wraps allocated memory suitable for cryptographic secrets.

When initialized with existing data, the memory of the existing data is zeroed out. That said, this library cannot guarantee that that memory has not been copied elsewhere, swapped to disk, or otherwise handled insecurely so rely on this with caution.

Examples

Random secrets:

use secrets::Secret;

let secret   = Secret::random(32);
let secret_r = secret.borrow();

println!("{:?}", secret_r.as_slice());

Secrets from existing mutable data:

use secrets::Secret;

let mut string   = "string".to_string();
let     secret   = Secret::from(unsafe { string.as_mut_vec() });
let     secret_r = secret.borrow();

assert_eq!("\0\0\0\0\0\0", string);
assert_eq!(b"string",      secret_r.as_slice());

Secrets as pointers:

use secrets::Secret;
use std::ptr;

let mut secret   = Secret::bytes(4);
let mut secret_w = secret.borrow_mut();

unsafe { ptr::write_bytes(secret_w.as_mut_ptr(), 0xff, secret_w.len()) };

assert_eq!([0xff, 0xff, 0xff, 0xff], secret_w.as_slice());

Methods

impl Secret<u8>
[src]

fn bytes(len: usize) -> Self

Creates a new Secret capable of storing len bytes.

By default, the allocated region is filled with 0xd0 bytes in order to help catch bugs due to uninitialized data.

fn random(len: usize) -> Self

Creates a new Secret filled with len bytes of cryptographically random data.

impl<T> Secret<T>
[src]

fn new(len: usize) -> Self

Creates a new Secret capable of storing len elements of type T.

By default, the allocated region is filled with 0xd0 bytes in order to help catch bugs due to uninitialized data.

fn len(&self) -> usize

Returns the number of elements in the Secret.

fn borrow(&self) -> Ref<T>

Returns a Ref<T> from which elements in the Secret can be safely read from, via either pointer or slice semantics.

fn borrow_mut(&mut self) -> RefMut<T>

Returns a Ref<T> from which elements in the Secret can be safely read from or written to, via either pointer or slice semantics.

Trait Implementations

impl<T: Debug> Debug for Secret<T>
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl<'a, T> From<&'a mut T> for Secret<u8> where T: BorrowMut<[u8]>
[src]

fn from(bytes: &'a mut T) -> Secret<u8>

Performs the conversion.

impl<T> PartialEq for Secret<T>
[src]

fn eq(&self, other: &Secret<T>) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Rhs) -> bool
1.0.0

This method tests for !=.

impl<T> Eq for Secret<T>
[src]