PBKDF2 (PKCS#5 v2.0 PBKDF2 Module)

1.3 · abandoned · verified Thu Apr 16

The `pbkdf2` library provides a Python implementation of the Password-Based Key Derivation Function 2 (PBKDF2), as specified in RSA PKCS#5 v2.0. It is designed to derive cryptographic keys from a password and a salt, leveraging iterative hashing to increase the computational cost for brute-force attacks. The library's last release was in June 2011, and while functional, modern Python applications are generally advised to use the built-in `hashlib.pbkdf2_hmac` function, which offers better performance and active maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates deriving a key using the `PBKDF2` class with a randomly generated salt and a high iteration count. It also shows the `crypt` helper function, though explicit use of `PBKDF2` is often preferred for control over the hashing algorithm and iterations.

import os
from pbkdf2 import PBKDF2

password = b"mysecretpassword"
salt = os.urandom(16) # Always use a unique, random salt
iterations = 100000 # Choose a high iteration count for security
dklen = 32 # Desired key length in bytes

# Derive a key using the PBKDF2 class
derived_key = PBKDF2(password, salt, iterations).read(dklen)

print(f"Salt: {salt.hex()}")
print(f"Derived Key: {derived_key.hex()}")

# Or use the simpler 'crypt' function (defaults to HMAC-SHA1 and specific format)
# NOTE: 'crypt' often uses a lower default iteration count and HMAC-SHA1, consider explicit PBKDF2 for modern security standards.
pwhash = PBKDF2.crypt(password.decode(), salt.decode(), iterations)
print(f"Password Hash (using .crypt): {pwhash}")

view raw JSON →