Argon2

Struct Argon2 

Source
pub struct Argon2 {
    pub m_cost: u32,
    pub t_cost: u32,
    pub p_cost: u32,
    pub hash_length: u64,
    pub algorithm: Algorithm,
    pub version: Version,
}
Expand description

Argon2 instance

§Parameters

  • m_cost - The memory cost in kibibytes
  • t_cost - Iteration cost
  • p_cost - Parallelization
  • hash_length - The length of the hash in bytes
  • algorithm - The algorithm to use
  • version - The version of the algorithm to use

By default it will use the Argon2id with a 64 byte hash length (maximum).

It is not recomended to change these specific values, they are fine for most use cases.

Generally speaking you don’t want to mess with the t_cost and p_cost parameters a lot.

§About the m_cost, t_cost and p_cost parameters

§m_cost

You should mostly adjust the m_cost if you really want to increase the security of the hash since this is the major bottleneck for GPUs and ASICs.

Anything from 1024_000 and beyond is considered very secure, if you are paranoid you should increase it to the max physical RAM of the machine this hash will be computed on.

§t_cost

For most use cases a good value is between 8 and 30.

Increasing the t_cost will increase the time it takes to compute the hash linearly.

For example if the hash takes 10 seconds to compute with t_cost set to 8 and you increase it to 16 it will take roughly twice the time.

§p_cost

For max security the p_cost should be set to 1.

Increasing the p_cost will decrease the time it takes to compute the hash linearly.

For example if the hash takes 10 seconds to compute with p_cost set to 1 and you increase it to 2 it will take roughly half the time.

Keep in mind increasing the p_cost beyond the machine’s physical cores will not increase the speed of the hash computation but in case of a brute-force attack the attacker will be able to use more cores to compute the hash and thus giving him leverage.

§Presets

There are some presets for the Argon2 struct that you can use.

  • Argon2::very_fast()
  • Argon2::fast()
  • Argon2::balanced()
  • Argon2::slow()
  • Argon2::very_slow()

Fields§

§m_cost: u32§t_cost: u32§p_cost: u32§hash_length: u64§algorithm: Algorithm

By default we use the Argon2id

§version: Version

By default we use the version 0x13

Implementations§

Source§

impl Argon2

Source

pub fn new(m_cost: u32, t_cost: u32, p_cost: u32) -> Argon2

Create a new Argon2 instance with the given parameters.

By default it will use the Argon2id with a 64 byte hash length.

§Arguments
  • m_cost - The memory cost in kibibytes
  • t_cost - Iteration cost
  • p_cost - Parallelization
Source

pub fn with_algorithm(self, algorithm: Algorithm) -> Argon2

Source

pub fn with_version(self, version: Version) -> Argon2

Source

pub fn with_hash_length(self, hash_length: u64) -> Argon2

Source

pub fn hash_password( &self, password: &str, salt: Vec<u8>, ) -> Result<Vec<u8>, Error>

Hashes the given password

§Arguments
  • password - The password to hash
  • salt - The salt to use for hashing
§Returns

The hash of the password in its raw byte form

Source§

impl Argon2

Source

pub fn very_fast() -> Argon2

Source

pub fn fast() -> Argon2

Source

pub fn balanced() -> Argon2

Source

pub fn slow() -> Argon2

Source

pub fn very_slow() -> Argon2

Trait Implementations§

Source§

impl<'__de, __Context> BorrowDecode<'__de, __Context> for Argon2

Source§

fn borrow_decode<__D>(decoder: &mut __D) -> Result<Argon2, DecodeError>
where __D: BorrowDecoder<'__de, Context = __Context>,

Attempt to decode this type with the given BorrowDecode.
Source§

impl Clone for Argon2

Source§

fn clone(&self) -> Argon2

Returns a duplicate 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 Debug for Argon2

Source§

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

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

impl<__Context> Decode<__Context> for Argon2

Source§

fn decode<__D>(decoder: &mut __D) -> Result<Argon2, DecodeError>
where __D: Decoder<Context = __Context>,

Attempt to decode this type with the given Decode.
Source§

impl Default for Argon2

Source§

fn default() -> Argon2

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

impl Encode for Argon2

Source§

fn encode<__E>(&self, encoder: &mut __E) -> Result<(), EncodeError>
where __E: Encoder,

Encode a given type.

Auto Trait Implementations§

§

impl Freeze for Argon2

§

impl RefUnwindSafe for Argon2

§

impl Send for Argon2

§

impl Sync for Argon2

§

impl Unpin for Argon2

§

impl UnwindSafe for Argon2

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.