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

source

pub fn new() -> HashBuilder

Create a new HashBuilder object with default parameters.

source

pub fn new_std(std: PasswordStorageStandard) -> HashBuilder

Create a new HashBuilder object with default parameters for a specific standard.

source

pub fn from_phc(data: &str) -> Result<Hasher, Error>

Create a new Hasher object from a PHC formatted string.

source

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.

source

pub fn finalize(&self) -> Result<Hasher, Error>

Check the compatibility between options and create a Hasher object.

source

pub fn normalization( &mut self, normalization: Normalization ) -> &mut HashBuilder

Set the way the password will be normalized.

source

pub fn algorithm(&mut self, algorithm: Algorithm) -> &mut HashBuilder

Set the password hashing algorithm.

source

pub fn length_calculation( &mut self, method: LengthCalculationMethod ) -> &mut HashBuilder

Set the way the password length will be calculated.

source

pub fn salt_len(&mut self, len: usize) -> &mut HashBuilder

Set the salt length.

Unused if a salt is given.

source

pub fn min_len(&mut self, len: usize) -> &mut HashBuilder

Set the password minimal length.

source

pub fn max_len(&mut self, len: usize) -> &mut HashBuilder

Set the password maximal length.

source

pub fn add_param(&mut self, key: &str, value: &str) -> &mut HashBuilder

Add a parameter that will be used by the password hashing algorithm.

source

pub fn version(&mut self, version: usize) -> &mut HashBuilder

Set the hashing scheme version number.

source

pub fn xhmac(&mut self, hash_func: HashFunction) -> &mut HashBuilder

Set the hash function that will be used to compute the additional HMAC.

source

pub fn xhmac_before(&mut self, pepper: &[u8]) -> &mut HashBuilder

Add an additional HMAC with a pepper before hashing the password.

source

pub fn xhmac_after(&mut self, pepper: &[u8]) -> &mut HashBuilder

Add an additional HMAC with a pepper after hashing the password.

Trait Implementations§

source§

impl Default for HashBuilder

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.