Secure Types
The goal of this crate is to provide a simple way to properly handle sensitive data in memory (eg. passwords, private keys, etc).
Currently there are 3 types:
SecureString: For working with strings.SecureVec: For working withVec<T>.SecureArray: For working with&[T; LENGTH].
Features
- Zeroization on Drop: Memory is wiped when dropped.
- Memory Locking: (OS-only) On Linux/Windows the memory is locked to prevent memory swapping or unauthorized access.
- Safe Scoped Access: Direct access on these types is not possible, data is protected by default and only accessible within safe blocks.
no_stdSupport: For embedded and Web environments (with zeroization only).- Serde Support: Optional serialization/deserialization.
How memory is locked
-
Windows: Using VirtualProtect & VirtualLock.
-
Linux: Using mlock & madvise If the kernel supports it, it will allocate with memfd_secret
Usage
SecureString
use SecureString;
// Create a SecureString
let mut secret = from;
// The memory is locked here
// Safely append more data.
secret.push_str;
// The memory is locked here.
// Use a scope to safely access the content as a &str.
secret.unlock_str;
// When `secret` is dropped, its data zeroized.
SecureVec
use SecureVec;
// Create a new, empty secure vector.
let mut secret_key: = new.unwrap;
// Push some sensitive data into it.
secret_key.push;
secret_key.push;
secret_key.push;
// The memory is locked here.
// Use a scope to safely access the contents as a slice.
secret_key.unlock_slice;
SecureArray
use SecureArray;
let exposed_array: &mut = &mut ;
let mut secure_array = from_slice_mut.unwrap;
secure_array.unlock_mut;
See also the examples.
Feature Flags
use_os(default): Enables all OS-level security features.no_os: Forno_stdenvironments. Only provides the Zeroize on Drop.serde: Enables serialization/deserialization.