pub struct SafeBox<T: ?Sized>(/* private fields */);
Expand description
A safe box for your secrets.
On Drop the content T is zeroed in RAM with memzero
.
It can only be instantiated with Copy types. This forbids instantiating a SafeBox<Vec<T>>
for
example, which cannot be zeroed.
&T access is guarded behind the unsafe get_ref
method. This prevents involuntary copies or
clone of the content. Deref is not implemented.
&mut T is also guarded behind unsafe get_mut
. This prevents involuntary memswap or memreplace
of the content. And because DerefMut is not implemented, the content cannot be moved out either.
Remember that it is perfectly safe to move or swap the SafeBox itself, because the content never
moves, merely the smart pointer details.
Because only Copy types are accepted for the content, it is possible to provide a safe implementation of Clone. It allocates a new SafeBox with a memcopy of the content.
It is implemented as a wrapper around a Box
Implementations§
Source§impl<T: Copy> SafeBox<T>
impl<T: Copy> SafeBox<T>
Sourcepub fn new(v: T) -> Self
pub fn new(v: T) -> Self
Allocate a new SafeBox from the given value.
Since v is passed by copy/move, it is advised to initialize with some safe value. Then use
SafeBox::get_mut
to write the secret value with the least amount of intermediate
copies.
Source§impl<T> SafeBox<[T]>
impl<T> SafeBox<[T]>
Sourcepub fn new_slice_with<F: Fn() -> T>(len: usize, f: F) -> Self
pub fn new_slice_with<F: Fn() -> T>(len: usize, f: F) -> Self
Allocate a new SafeBox<[T]>
.
The function f
is called to initialize the len
elements.
use safebox::SafeBox;
use rand::prelude::*;
let random_secret = SafeBox::new_slice_with(8, &random::<u8>);