Struct argon2_kdf::Hasher

source ·
pub struct Hasher<'a> { /* private fields */ }
Expand description

A builder for a hash. Parameters for hashing, such as

Implementations§

source§

impl<'a> Hasher<'a>

source

pub fn new() -> Self

Create a new Hasher with default values.

This provides some reasonable defaults, but it is recommended that you tinker with these parameters to find the best settings for your application. The more resources the hashing requires, the stronger the hash. Increase the memory cost (and perhaps the parallelization factor) as high as your application can afford, then likewise raise the iteration count.

Unless you are absolutely positive you want to use a different algorithm, use the default argon2id algorithm for password hashing and key derivation.

The defaults are as follows:

  • Algorithm: Argon2id
  • Salt Length: 16 bytes
  • Hash Length: 32 bytes
  • Iterations: 18
  • Memory Cost: 62500 kibibytes (equal to 64 megabytes)
  • Parallelization Factor: 1 thread

Hasher allows for a secret, sometimes called a “pepper,” to be mixed with the password before hashing. Hasher can be used securely without a secret, though high-security applications might consider using one.

source

pub fn algorithm(self, alg: Algorithm) -> Self

Specifies the hashing algorithm to use.

The Argon2 spec consist of 3 different algorithms: one that aims to be resistant to GPU cracking attacks (argon2d), one that aims to be resistant to side-channel attacks (argon2i), and a hybrid algorithm that aims to be resistant to both types of attacks. See https://en.wikipedia.org/wiki/Argon2 for more information.

Argon2id is a good default. The other algorithms should only be used in rare cases, preferably only when a cryptography expert can validate that using one of the other two algorithms is safe.

source

pub fn custom_salt(self, salt: &'a [u8]) -> Self

When left unspecified, a salt is generated using a cryptographically-secure random number generator. In most cases, this function should not be used. Only use this function if you are trying to generate a hash deterministically with a known salt and a randomly generated salt will not suffice.

source

pub fn salt_length(self, salt_len: u32) -> Self

The length of the salt for the hash, in bytes. Using salt that is too short can lower the strength of the generated hash. 16 bytes is a reasonable default salt length.

If a salt is specified manually using [custom_salt()], the length of the provided salt will override the length specified here.

source

pub fn hash_length(self, hash_len: u32) -> Self

The length of the resulting hash, in bytes.

Short hashes can be insecure. The shorter the hash, the greater the chance of collisions. A 32-byte hash should be plenty for any application.

Note that the length of the hash string will be different; the hash string specifies parameters and the salt used to generate the hash. The hash is base64-encoded in the hash string, so even the hash itself is longer in the hash string than the specified number of bytes.

A hash is just an array of bytes, whereas a hash string looks something like this:

$argon2id$v=19$m=62500,t=18,p=2$AQIDBAUGBwg$ypJ3pKxN4aWGkwMv0TOb08OIzwrfK1SZWy64vyTLKo8

source

pub fn iterations(self, iterations: u32) -> Self

The number of times the hashing algorithm is repeated in order to slow down the hashing and thwart those pesky hackers.

source

pub fn memory_cost_kib(self, cost: u32) -> Self

The amount of memory required to compute a hash. This is where a lot of the magic of Argon2 happens. By setting a hard memory requirement for generating a hash, brute-forcing a password becomes infeasable even for well-funded adversaries with access to a lot of processing power.

Set this parameter as high as you can afford to. Be cautious setting this lower than 62500 KiB (64 MB). If reasonable, increase this to 125000 KiB (128 MB) or 250000 KiB (256 MB) (or even higher were security is critical).

source

pub fn threads(self, threads: u32) -> Self

The number of CPU threads required to generate a hash. If this is set higher than the total number of logical CPU cores on a given machine, hashing may fail or take an astronomically long time to generate on said machine.

While increasing the thread count does strengthen the hash, it is impractical to raise this parameter for some applications. Aim to increase the memory cost before increasing the thread count. With a high memory cost, just 1 thread can still provide excellent security.

source

pub fn secret(self, secret: Secret<'a>) -> Self

A secret that mixes with a password (and a salt) to create a hash. This is sometimes referred to as a “pepper.”

This secret is not necessary to generate strong hashes, though high-security applications might consider using a secret. Many argon2 libraries don’t expose this parameter (because it isn’t necessary), so using a secret can limit interoperability with other languages/libraries.

A 32-byte key is recommended. Do not use an alphanumeric password or passphrase; the entrophy of a 32-character password is much lower than the entrophy of a 32-byte key. This key should be generated with a cryptographically-secure random number generator and stored securely.

source

pub fn hash(self, password: &[u8]) -> Result<Hash, Argon2Error>

Consumes the Hasher and returns a hash.

This is an expensive operation. For some appliations, it might make sense to move this operation to a separate thread using std::thread or something like the Rayon crate to avoid blocking main threads.

Trait Implementations§

source§

impl<'a> Clone for Hasher<'a>

source§

fn clone(&self) -> Hasher<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Hasher<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Hasher<'_>

source§

fn default() -> Self

Create a new Hasher with default values.

This provides some reasonable defaults, but it is recommended that you tinker with these parameters to find the best settings for your application. The more resources the hashing requires, the stronger the hash. Increase the memory cost (and perhaps the parallelization factor) as high as your application can afford, then likewise raise the iteration count.

Unless you are absolutely positive you want to use a different algorithm, use the default argon2id algorithm for password hashing and key derivation.

The defaults are as follows:

  • Algorithm: Argon2id
  • Salt Length: 16 bytes
  • Hash Length: 32 bytes
  • Iterations: 18
  • Memory Cost: 62500 kibibytes (equal to 64 megabytes)
  • Parallelization Factor: 1 thread

Hasher allows for a secret, sometimes called a “pepper,” to be mixed with the password before hashing. Hasher can be used securely without a secret, though high-security applications might consider using one.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Hasher<'a>

§

impl<'a> Send for Hasher<'a>

§

impl<'a> Sync for Hasher<'a>

§

impl<'a> Unpin for Hasher<'a>

§

impl<'a> UnwindSafe for Hasher<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V