Struct libreauth::pass::HashBuilder
source · pub struct HashBuilder { /* private fields */ }
Expand description
Builds a Hasher object.
Examples
use libreauth::pass::HashBuilder;
// Hashing a password in order to store it.
let password = "correct horse battery staple";
let hasher = match HashBuilder::new().finalize() {
Ok(h) => h,
Err(e) => panic!("{:?}", e),
};
let stored_password = match hasher.hash(password) {
Ok(p) => p,
Err(e) => panic!("{:?}", e),
};
// Checking a password against a previously hashed one.
let checker = HashBuilder::from_phc(stored_password.as_str()).unwrap();
assert!(!checker.is_valid("bad password"));
assert!(checker.is_valid(password));
Build a Hasher object with the default parameters to comply with the NIST Special Publication 800-63B. This object will be usable to hash a password.
use libreauth::pass::{HashBuilder, PasswordStorageStandard};
let hasher = match HashBuilder::new_std(PasswordStorageStandard::Nist80063b).finalize() {
Ok(h) => h,
Err(e) => panic!("{:?}", e),
};
Build a Hasher object with custom parameters. This object will be usable to hash a password.
let hasher = match libreauth::pass::HashBuilder::new()
.min_len(12)
.algorithm(libreauth::pass::Algorithm::Pbkdf2)
.add_param("hmac", "sha256")
.add_param("norm", "nfkd")
.finalize() {
Ok(h) => h,
Err(e) => panic!("{:?}", e),
};
Implementations§
source§impl HashBuilder
impl HashBuilder
sourcepub fn new() -> HashBuilder
pub fn new() -> HashBuilder
Create a new HashBuilder object with default parameters.
sourcepub fn new_std(std: PasswordStorageStandard) -> HashBuilder
pub fn new_std(std: PasswordStorageStandard) -> HashBuilder
Create a new HashBuilder object with default parameters for a specific standard.
sourcepub fn from_phc(data: &str) -> Result<Hasher, Error>
pub fn from_phc(data: &str) -> Result<Hasher, Error>
Create a new Hasher object from a PHC formatted string.
sourcepub fn from_phc_xhmac(data: &str, pepper: &[u8]) -> Result<Hasher, Error>
pub fn from_phc_xhmac(data: &str, pepper: &[u8]) -> Result<Hasher, Error>
Create a new Hasher object from a PHC formatted string and an external pepper for an additional HMAC.
sourcepub fn finalize(&self) -> Result<Hasher, Error>
pub fn finalize(&self) -> Result<Hasher, Error>
Check the compatibility between options and create a Hasher object.
sourcepub fn normalization(
&mut self,
normalization: Normalization
) -> &mut HashBuilder
pub fn normalization( &mut self, normalization: Normalization ) -> &mut HashBuilder
Set the way the password will be normalized.
sourcepub fn algorithm(&mut self, algorithm: Algorithm) -> &mut HashBuilder
pub fn algorithm(&mut self, algorithm: Algorithm) -> &mut HashBuilder
Set the password hashing algorithm.
sourcepub fn length_calculation(
&mut self,
method: LengthCalculationMethod
) -> &mut HashBuilder
pub fn length_calculation( &mut self, method: LengthCalculationMethod ) -> &mut HashBuilder
Set the way the password length will be calculated.
sourcepub fn salt_len(&mut self, len: usize) -> &mut HashBuilder
pub fn salt_len(&mut self, len: usize) -> &mut HashBuilder
Set the salt length.
Unused if a salt is given.
sourcepub fn min_len(&mut self, len: usize) -> &mut HashBuilder
pub fn min_len(&mut self, len: usize) -> &mut HashBuilder
Set the password minimal length.
sourcepub fn max_len(&mut self, len: usize) -> &mut HashBuilder
pub fn max_len(&mut self, len: usize) -> &mut HashBuilder
Set the password maximal length.
sourcepub fn add_param(&mut self, key: &str, value: &str) -> &mut HashBuilder
pub fn add_param(&mut self, key: &str, value: &str) -> &mut HashBuilder
Add a parameter that will be used by the password hashing algorithm.
sourcepub fn version(&mut self, version: usize) -> &mut HashBuilder
pub fn version(&mut self, version: usize) -> &mut HashBuilder
Set the hashing scheme version number.
sourcepub fn xhmac(&mut self, hash_func: HashFunction) -> &mut HashBuilder
pub fn xhmac(&mut self, hash_func: HashFunction) -> &mut HashBuilder
Set the hash function that will be used to compute the additional HMAC.
sourcepub fn xhmac_before(&mut self, pepper: &[u8]) -> &mut HashBuilder
pub fn xhmac_before(&mut self, pepper: &[u8]) -> &mut HashBuilder
Add an additional HMAC with a pepper before hashing the password.
sourcepub fn xhmac_after(&mut self, pepper: &[u8]) -> &mut HashBuilder
pub fn xhmac_after(&mut self, pepper: &[u8]) -> &mut HashBuilder
Add an additional HMAC with a pepper after hashing the password.