Skip to main content

Params

Struct Params 

Source
pub struct Params { /* private fields */ }
Expand description

Validated Argon2 cost parameters.

Holds exactly the fields of argon2_context that are not byte buffers: m_cost, t_cost, lanes, threads and outlen. Password, salt, secret and associated data are passed per call.

There is no public field and no public constructor: every Params comes out of ParamsBuilder::build, which runs validate_inputs, or out of a preset (Params::DEFAULT, Params::OWASP, Params::RFC9106_HIGH_MEMORY, Params::RFC9106_LOW_MEMORY) that build’s const twin already ran. So lanes >= 1 always holds and the derived values below never divide by zero.

Start from Params::builder, or from Params::to_builder to adjust an existing value.

§Known divergence from the C reference

ParamsBuilder::build validates the cost parameters immediately, whereas the C checks salt length before m_cost. If a caller supplies both a bad m_cost and a short salt, this crate reports the m_cost error at Params construction time while the C reports ARGON2_SALT_TOO_SHORT. Call validate_inputs directly to reproduce the C ordering exactly.

Implementations§

Source§

impl Params

Source

pub const DEFAULT: Params

The recommended default: OWASP’s Argon2id profile.

19 MiB, two passes, one lane, a 32-byte tag. Equal to Params::OWASP and to Params::default().

Source

pub const OWASP: Params = Params::DEFAULT

OWASP’s Argon2id profile: 19 MiB, t=2, p=1, 32-byte tag.

The same value as Params::DEFAULT, under the name that says where the numbers come from.

Source

pub const RFC9106_HIGH_MEMORY: Params

RFC 9106 §4’s first recommendation: 2 GiB, t=1, p=4, 32-byte tag.

On a 32-bit target 2 GiB is exactly MAX_MEMORY, so this constant still compiles there — but the arena will fail to allocate inside a 4 GiB address space. Prefer Params::RFC9106_LOW_MEMORY there.

Source

pub const RFC9106_LOW_MEMORY: Params

RFC 9106 §4’s second recommendation, for memory-constrained systems: 64 MiB, t=3, p=4, 32-byte tag.

Source

pub const fn builder() -> ParamsBuilder

Start building, from ParamsBuilder::DEFAULT.

Source

pub const fn to_builder(self) -> ParamsBuilder

Reopen these parameters for adjustment.

use argon2_rust::Params;

let narrow = Params::RFC9106_LOW_MEMORY.to_builder().lanes(1).build()?;
assert_eq!(narrow.lanes(), 1);
Source

pub const fn memory(&self) -> Memory

The memory cost.

Source

pub const fn memory_kib(&self) -> u32

The memory cost in kibibytes (context.m_cost, m= in a PHC string).

A u32, losslessly: build() rejected anything wider.

Source

pub const fn passes(&self) -> u32

Number of passes (context.t_cost, instance.passes, t=).

Source

pub const fn tag_len(&self) -> TagLen

The tag length.

Source

pub const fn tag_len_bytes(&self) -> usize

The tag length in bytes (context.outlen).

A usize, losslessly: build() rejected anything wider.

Source

pub const fn validate_for( &self, pwd_len: usize, salt_len: usize, secret_len: usize, ad_len: usize, ) -> Result<(), Error>

Run the full validate_inputs() sequence for a concrete call.

core calls this on every hash so the salt/password/secret/ad checks fire in the C’s order.

§Errors

Any error from validate_inputs.

Source

pub const fn lanes(&self) -> u32

Degree of parallelism (context.lanes). Affects the tag.

Source

pub const fn threads(&self) -> u32

Requested worker threads (context.threads). Does not affect the tag.

Source

pub const fn effective_threads(&self) -> u32

min(threads, lanes), as argon2_ctx computes it.

Source

pub const fn memory_layout(&self) -> (u32, u32, u32)

Step 2 of argon2_ctx(): align the memory size.

memory_blocks = m_cost;
if (memory_blocks < 2 * SYNC_POINTS * lanes)
    memory_blocks = 2 * SYNC_POINTS * lanes;
segment_length = memory_blocks / (lanes * SYNC_POINTS);
memory_blocks  = segment_length * (lanes * SYNC_POINTS);
lane_length    = segment_length * SYNC_POINTS;

Returns (memory_blocks, segment_length, lane_length). No overflow is possible: lanes <= MAX_LANES (0xFF_FFFF), so lanes * SYNC_POINTS fits comfortably in u32, and segment_length * lanes * SYNC_POINTS is bounded by the original memory_blocks <= MAX_MEMORY.

Source

pub const fn memory_blocks(&self) -> u32

Number of 1 KiB blocks the arena needs (instance.memory_blocks).

Source

pub const fn segment_length(&self) -> u32

Blocks per segment (instance.segment_length).

Source

pub const fn lane_length(&self) -> u32

Blocks per lane (instance.lane_length = segment_length * SYNC_POINTS).

Trait Implementations§

Source§

impl Clone for Params

Source§

fn clone(&self) -> Params

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Params

Source§

impl Debug for Params

Source§

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

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

impl Default for Params

Source§

fn default() -> Params

Params::DEFAULT: OWASP’s profile, 19 MiB, t=2, p=1, 32-byte tag.

Source§

impl Eq for Params

Source§

impl Hash for Params

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Params

Source§

fn eq(&self, other: &Params) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Params

Auto Trait Implementations§

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