Skip to main content

ParamsBuilder

Struct ParamsBuilder 

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

Builder for Params.

Every setter is a const fn taking self by value, so a Params can be built in a const item — see ParamsBuilder::build_or_panic. The builder starts from ParamsBuilder::DEFAULT, so each setter is optional.

use argon2_rust::{Params, params::{Memory, TagLen}};

let params = Params::builder()
    .memory(Memory::mib(64))
    .passes(3)
    .lanes(4)
    .tag_len(TagLen::bytes(32))
    .build()?;
assert_eq!(params.memory(), Memory::mib(64));

Implementations§

Source§

impl ParamsBuilder

Source

pub const DEFAULT: ParamsBuilder

The starting point: OWASP’s Argon2id profile, 19 MiB, two passes, one lane, a 32-byte tag.

These are literals rather than a copy of Params::DEFAULT’s fields on purpose. Params::DEFAULT is built by this builder, so reading it here would be a cyclic const.

Source

pub const fn memory(self, memory: Memory) -> ParamsBuilder

Set the memory cost.

Source

pub const fn passes(self, passes: u32) -> ParamsBuilder

Set the number of passes (t_cost in the C, t= in a PHC string).

Source

pub const fn lanes(self, lanes: u32) -> ParamsBuilder

Set the degree of parallelism (p= in a PHC string).

This one feeds the tag. Changing it changes the hash.

Source

pub const fn threads(self, threads: u32) -> ParamsBuilder

Set the worker-thread budget.

A pure performance knob: it does not affect the tag. Only lanes does. Left unset it tracks ParamsBuilder::lanes, which is what argon2_hash() does — it sets both context.lanes and context.threads from its single parallelism argument. The effective count is min(threads, lanes), see Params::effective_threads.

use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};

// Four lanes of work, but never more than two OS threads to run them.
let budgeted = Params::builder()
    .memory(Memory::kib(64))
    .passes(1)
    .lanes(4)
    .threads(2)
    .build()?;
assert_eq!((budgeted.lanes(), budgeted.threads()), (4, 2));
assert_eq!(budgeted.effective_threads(), 2);

// Asking for more threads than lanes is legal, and the extra workers
// simply have no lane to claim.
let oversubscribed = Params::builder()
    .memory(Memory::kib(64))
    .passes(1)
    .lanes(2)
    .threads(8)
    .build()?;
assert_eq!(oversubscribed.effective_threads(), 2);

// Leaving it unset is exactly `threads == lanes`.
let full = Params::builder()
    .memory(Memory::kib(64))
    .passes(1)
    .lanes(4)
    .build()?;
assert_eq!(full, budgeted.to_builder().threads(4).build()?);
assert_eq!(full.threads(), 4);

// And the knob really is free of the tag: same `lanes`, same bytes,
// whichever thread budget produced them.
let two_workers = Argon2::new(Algorithm::Argon2id, Version::V0x13, budgeted);
let four_workers = Argon2::new(Algorithm::Argon2id, Version::V0x13, full);
assert_eq!(
    two_workers.hash(b"password", b"somesalt")?,
    four_workers.hash(b"password", b"somesalt")?,
);
Source

pub const fn tag_len(self, tag_len: TagLen) -> ParamsBuilder

Set the tag length.

Source

pub const fn build(self) -> Result<Params, Error>

Validate and produce Params.

§Errors

Any of the cost-parameter errors from validate_inputs, plus Error::OutputTooLong and Error::MemoryTooMuch for values too large for this target at all.

Source

pub const fn build_or_panic(self) -> Params

Validate and produce Params, panicking on invalid parameters.

This exists for const items, where a panic is a compile error:

use argon2_rust::{Params, params::Memory};

const LOGIN: Params = Params::builder()
    .memory(Memory::mib(64))
    .passes(3)
    .build_or_panic();
assert_eq!(LOGIN.passes(), 3);
§Panics

If the parameters are invalid. Use ParamsBuilder::build anywhere a runtime error is the right answer — it is the normal way in, and it is why no fallible path in this crate panics.

Trait Implementations§

Source§

impl Clone for ParamsBuilder

Source§

fn clone(&self) -> ParamsBuilder

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 ParamsBuilder

Source§

impl Debug for ParamsBuilder

Source§

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

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

impl Default for ParamsBuilder

Source§

fn default() -> ParamsBuilder

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

impl Eq for ParamsBuilder

Source§

impl Hash for ParamsBuilder

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 ParamsBuilder

Source§

fn eq(&self, other: &ParamsBuilder) -> 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 ParamsBuilder

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.