pwdlib: Modern Password Hashing for Python
pwdlib is a modern password hashing library for Python, providing an easy-to-use wrapper to hash and verify passwords with secure algorithms like Argon2 and Bcrypt. It aims to be an alternative to `passlib`, which has seen reduced maintenance. The current version is 0.3.0, and it maintains an active development status, with updates released as needed.
Warnings
- breaking Python 3.9 is no longer supported as of version 0.3.0. Users on Python 3.9 must upgrade their Python version to 3.10 or later.
- breaking In version 0.2.0, the argument order for `PasswordHash.verify()` and `PasswordHash.verify_and_update()` methods was reversed. The password is now the *first* argument, and the hash is the *second* argument, for consistency with `passlib`'s API. [cite: original text]
- gotcha `pwdlib` is not a direct, drop-in replacement for `passlib`. While inspired by `passlib`, it focuses on modern algorithms (Argon2, Bcrypt) and does not support many legacy hashing algorithms or advanced `CryptContext` features found in `passlib`.
- gotcha The `PasswordHash.recommended()` method currently defaults to Argon2. If you need to explicitly use Bcrypt or a different configuration of hashers, you must instantiate `PasswordHash` with a sequence of `HasherProtocol` objects.
Install
-
pip install 'pwdlib[argon2]' -
pip install 'pwdlib[bcrypt]' -
pip install pwdlib
Imports
- PasswordHash
from pwdlib import PasswordHash
Quickstart
from pwdlib import PasswordHash
# Get a PasswordHash instance with recommended hashers (currently Argon2)
password_hash = PasswordHash.recommended()
# Hash a password
hashed_password = password_hash.hash("mysecretpassword")
print(f"Hashed password: {hashed_password}")
# Verify a password
is_valid = password_hash.verify("mysecretpassword", hashed_password)
print(f"Password is valid: {is_valid}")
# Verify and update (if hasher or hash needs upgrade)
is_valid_and_updated, new_hash = password_hash.verify_and_update("mysecretpassword", hashed_password)
print(f"Password valid and potentially updated: {is_valid_and_updated}, New hash: {new_hash}")