Skip to Content
oxcrypto

PBKDF2

Derive keys or store password records with HMAC over SHA-1, SHA-2 or SHA-3.

from secrets import token_bytes

from oxcrypto.password.pbkdf2 import PBKDF2

algorithm = PBKDF2.PBKDF2_SHA256
key = algorithm.derive(b"passphrase", salt=token_bytes(16), rounds=600_000, length=32)

derive is available for every variant:

VariantPRFhash / verify
PBKDF2_SHA1HMAC-SHA-1 (legacy)no
PBKDF2_SHA224, PBKDF2_SHA384HMAC-SHA-224, HMAC-SHA-384no
PBKDF2_SHA256HMAC-SHA-256yes ($pbkdf2-sha256$)
PBKDF2_SHA512HMAC-SHA-512yes ($pbkdf2-sha512$)
PBKDF2_SHA512_224, PBKDF2_SHA512_256HMAC-SHA-512/224, HMAC-SHA-512/256no
PBKDF2_SHA3_224 … PBKDF2_SHA3_512HMAC-SHA3-224 … HMAC-SHA3-512no

The pbkdf2 crate defines PHC records only for SHA-256 and SHA-512, so hash raises ValueError for the other variants and verify returns False. For key derivation, keep the selected variant, salt, and rounds with the encrypted data.

hash takes a keyword-only rounds (at least 1,000). The default is the crate's recommendation: 600,000 for SHA-256 and 210,000 for SHA-512.

encoded = PBKDF2.PBKDF2_SHA256.hash(b"passphrase", rounds=1_000_000)
assert PBKDF2.PBKDF2_SHA256.verify(b"passphrase", encoded)

PBKDF2 is specified in RFC 8018 (Informational) and NIST SP 800-132. The implementation is the RustCrypto pbkdf2 crate 0.13.0 with hmac 0.13 (SimpleHmac for SHA-3, whose hashers have no eager block core), sha1 0.11, sha2 0.11 and sha3 0.12; all are stable releases with no published audit. Tests use the RFC 6070 SHA-1 vector and Python hashlib.pbkdf2_hmac outputs for the other digests.