argon2-cffi

25.1.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `PasswordHasher`, hash a password, verify it, and check if the hash needs to be updated due to changed parameters. Always catch `VerifyMismatchError` for password mismatches.

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.")

view raw JSON →