std/digest/pbkdf2

std/digest/src/pbkdf2.trb

Hmac.pbkdf2: PBKDF2 (RFC 8018) over any Hasher through Hmac - the password hash SCRAM authenticates with, and NIST's key derivation function for a password or a passphrase.

extend Hmac<H>

extend<H: Hasher> Hmac<H>

The key derivation of PBKDF2 over any Hmac, so over any Hasher: Hmac.pbkdf2.

fn pbkdf2

static fn pbkdf2(password: Bytes, salt: Bytes, iterations: Int, length: Int): Bytes

password stretched into length bytes: iterations rounds of HMAC, each block salted with salt and its own big-endian index (RFC 8018 section 5.2), the rounds of one block XORed together into it.

Fast at the word level. Hmac.keyed runs once, before the rounds - not once a round, which is where a naive PBKDF2 spends most of its time re-hashing the key's two blocks. A round after the first copies that keyed state (its size, not its history) and hashes one digest's worth of bytes: iterations rounds cost iterations hashes of one small message, never iterations hashes of the key.

Examples

const derived = HmacSha256.pbkdf2 "password".bytes(), "salt".bytes(), 4096, 32