Skip to main content

derive

Function derive 

Source
pub fn derive<H: Hasher, const N: usize>(
    password: &[u8],
    salt: &[u8],
    iterations: u32,
) -> [u8; N]
Expand description

Derives a key using PBKDF2-HMAC with the given hash function.

PBKDF2 applies the HMAC-based PRF repeatedly (iterations times) to produce a derived key of N bytes.

⚠️ PBKDF2 is not memory-hard, making it vulnerable to GPU/ASIC-based brute-force attacks. For password hashing, prefer [crate::argon2::Argon2id] unless PBKDF2 is required for legacy compatibility or specific protocol standards.

§Example

use crypto::pbkdf2;
use crypto::sha2::Sha256;

let key: [u8; 32] = pbkdf2::derive::<Sha256, 32>(
    b"password",
    b"salt",
    4096,
);

§Panics

derive panics if iterations == 0 (iterations must be >= 1) or N > (2^32 - 1) * H::OUTPUT_SIZE (output length exceeds RFC 2898 limit).