PBKDF2 (PKCS#5 v2.0 PBKDF2 Module)
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
-
PBKDF2 output differs across platforms/languages (e.g., Python vs C#)
cause Inconsistent parameters (hash algorithm, salt encoding, iteration count, key length) or byte-string handling between implementations. Forgetting that `pbkdf2` often defaults to SHA-1 or different byte representations.fixEnsure all PBKDF2 parameters (password, salt, iterations, desired key length, and HMAC hash algorithm) are identical across implementations. Convert passwords and salts to byte strings consistently (e.g., `password.encode('utf-8')`). Explicitly set the hash algorithm, e.g., 'sha256'. Confirm the derived key length (`dklen`) is the same. Python's `hashlib.pbkdf2_hmac` is often easier to synchronize. -
AttributeError: 'module' object has no attribute 'PBKDF2' or 'crypt'
cause Incorrect import statement or a different `pbkdf2` package being installed/imported. Some systems might have other `pbkdf2` related modules or older versions where `PBKDF2` might be nested.fixVerify the correct installation with `pip show pbkdf2` and check the import: `from pbkdf2 import PBKDF2` or `from pbkdf2 import crypt`. If `pbkdf2.py` is directly in the path, it might be picked up instead of the installed package. Consider using the fully qualified name `pbkdf2.PBKDF2` or `pbkdf2.crypt` if `from pbkdf2 import ...` causes issues due to other modules named `pbkdf2`. -
TypeError: ('expected bytes, got str',)cause The `pbkdf2` functions expect byte strings for `password` and `salt`, but a Python 3 `str` (unicode) object was provided. This is a common Python 2 to 3 migration issue.fixEncode the password and salt to byte strings before passing them to `PBKDF2` or `crypt`. For example: `password.encode('utf-8')` and `salt.encode('utf-8')` (if salt is a string) or `os.urandom(16)` for binary salt.
Warnings
- deprecated This `pbkdf2` package has not been updated since 2011. Python's standard library `hashlib` module provides `hashlib.pbkdf2_hmac`, which is the recommended, actively maintained, and often more performant way to perform PBKDF2.
- gotcha The `pbkdf2` library's default hashing algorithm for `PBKDF2` can be HMAC-SHA1 if not explicitly specified, which is less secure than SHA-256 or SHA-512 for password hashing in modern applications.
- gotcha Using a low iteration count (rounds) severely weakens the security against brute-force attacks. Older examples or default values might be dangerously low for current computing power.
- gotcha Failing to use a unique, cryptographically secure random salt for each password or not storing the salt alongside the hash is a critical security vulnerability. Reusing salts or using predictable salts enables rainbow table attacks.
Install
-
pip install pbkdf2
Imports
- PBKDF2
from pbkdf2 import PBKDF2
- crypt
from pbkdf2 import crypt
Quickstart
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}")