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
impl Params
Sourcepub const DEFAULT: Params
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().
Sourcepub const OWASP: Params = Params::DEFAULT
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.
Sourcepub const RFC9106_HIGH_MEMORY: Params
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.
Sourcepub const RFC9106_LOW_MEMORY: Params
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.
Sourcepub const fn builder() -> ParamsBuilder
pub const fn builder() -> ParamsBuilder
Start building, from ParamsBuilder::DEFAULT.
Sourcepub const fn to_builder(self) -> ParamsBuilder
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);Sourcepub const fn memory_kib(&self) -> u32
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.
Sourcepub const fn tag_len_bytes(&self) -> usize
pub const fn tag_len_bytes(&self) -> usize
The tag length in bytes (context.outlen).
A usize, losslessly: build() rejected anything wider.
Sourcepub const fn validate_for(
&self,
pwd_len: usize,
salt_len: usize,
secret_len: usize,
ad_len: usize,
) -> Result<(), Error>
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.
Sourcepub const fn threads(&self) -> u32
pub const fn threads(&self) -> u32
Requested worker threads (context.threads). Does not affect the tag.
Sourcepub const fn effective_threads(&self) -> u32
pub const fn effective_threads(&self) -> u32
min(threads, lanes), as argon2_ctx computes it.
Sourcepub const fn memory_layout(&self) -> (u32, u32, u32)
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.
Sourcepub const fn memory_blocks(&self) -> u32
pub const fn memory_blocks(&self) -> u32
Number of 1 KiB blocks the arena needs (instance.memory_blocks).
Sourcepub const fn segment_length(&self) -> u32
pub const fn segment_length(&self) -> u32
Blocks per segment (instance.segment_length).
Sourcepub const fn lane_length(&self) -> u32
pub const fn lane_length(&self) -> u32
Blocks per lane (instance.lane_length = segment_length * SYNC_POINTS).
Trait Implementations§
impl Copy for Params
Source§impl Default for Params
impl Default for Params
Source§fn default() -> Params
fn default() -> Params
Params::DEFAULT: OWASP’s profile, 19 MiB, t=2, p=1, 32-byte tag.