Crates.io | secstr |
lib.rs | secstr |
version | 0.5.1 |
source | src |
created_at | 2015-09-09 15:23:05.168259 |
updated_at | 2022-10-02 22:51:49.638605 |
description | A data type suitable for storing sensitive information such as passwords and private keys in memory, featuring constant time equality, mlock and zeroing out. |
homepage | https://codeberg.org/valpackett/secstr |
repository | https://codeberg.org/valpackett/secstr |
max_upload_size | |
id | 3009 |
size | 51,136 |
A Rust library that implements a data type (wrapper around Vec<u8>
) suitable for storing sensitive information such as passwords and private keys in memory.
Inspired by Haskell securemem and .NET SecureString.
Featuring:
mlock
and madvise
protection if possible***SECRET***
to prevent leaking into logsstd::hash::Hash
)unsafe
APIextern crate secstr;
use secstr::*;
let pw = SecStr::from("correct horse battery staple");
// Compared in constant time:
// (Obviously, you should store hashes in real apps, not plaintext passwords)
let are_pws_equal = pw == SecStr::from("correct horse battery staple".to_string()); // true
// Formatting, printing without leaking secrets into logs
let text_to_print = format!("{}", SecStr::from("hello")); // "***SECRET***"
// Clearing memory
// THIS IS DONE AUTOMATICALLY IN THE DESTRUCTOR
// (but you can force it)
let mut my_sec = SecStr::from("hello");
my_sec.zero_out();
// (It also sets the length to 0)
assert_eq!(my_sec.unsecure(), b"");
Be careful with SecStr::from
: if you have a borrowed string, it will be copied.
Use SecStr::new
if you have a Vec<u8>
.
Please feel free to submit pull requests!
By participating in this project you agree to follow the Contributor Code of Conduct and to release your contributions under the Unlicense.
The list of contributors is available on GitHub.
This is free and unencumbered software released into the public domain.
For more information, please refer to the UNLICENSE
file or unlicense.org.