argon2-cffi
argon2-cffi is a Python library that provides secure password hashing using the Argon2 algorithm, the winner of the Password Hashing Competition. It offers a high-level API for hashing and verification, along with options to tune security parameters. The library is actively maintained and releases new versions with performance improvements, enhanced platform support, and updated RFC compliance.
Warnings
- breaking Python 2.7, 3.4, 3.5, and 3.7 are no longer supported. The minimum Python version is now 3.8.
- breaking The legacy top-level functions `argon2.hash_password()`, `argon2.hash_password_raw()`, and `argon2.verify_password()` that were soft-deprecated since 2016 and hard-deprecated (raising `DeprecationWarning`) in 23.1.0 are now removed.
- breaking Default hashing parameters for `PasswordHasher` changed in version 21.2.0 to align with RFC 9106's low-memory profile. While old hashes remain verifiable, new hashes will use the updated, more secure defaults.
- breaking Since version 21.2.0, the CFFI bindings were extracted into `argon2-cffi-bindings`. This is a breaking change for users attempting to use a system-wide installation of Argon2 with `--no-binary`, as the argument value changed. Most users relying on default `pip install` with vendored code are unaffected.
- gotcha Forgetting to specifically catch `argon2.exceptions.VerifyMismatchError` during password verification. This exception is raised when a submitted password does not match the stored hash, indicating a failed login attempt.
- gotcha The `salt` parameter was added to `argon2.PasswordHasher.hash()` in v23.1.0. While available, it's generally not recommended to provide your own salt unless you have a very specific, advanced use case. The library generates secure, random salts by default.
Install
-
pip install argon2-cffi
Imports
- PasswordHasher
from argon2 import PasswordHasher
- VerifyMismatchError
from argon2.exceptions import VerifyMismatchError
Quickstart
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
ph = PasswordHasher()
# Hash a password
password = "correct horse battery staple"
hashed_password = ph.hash(password)
print(f"Hashed: {hashed_password}")
# Verify a password
try:
ph.verify(hashed_password, password)
print("Verification successful!")
except VerifyMismatchError:
print("Verification failed: Password does not match.")
except Exception as e:
print(f"An unexpected error occurred during verification: {e}")
# Check if a rehash is needed (e.g., if parameters changed)
if ph.check_needs_rehash(hashed_password):
print("Password needs re-hashing with new parameters.")
else:
print("Password hash parameters are up-to-date.")