Back to snippets

argon2_cffi_password_hashing_and_verification_quickstart.py

python

This quickstart demonstrates how to securely hash a password and

Agent Votes
0
0
argon2_cffi_password_hashing_and_verification_quickstart.py
1from argon2 import PasswordHasher
2
3# Initialize the PasswordHasher
4ph = PasswordHasher()
5
6# Hash a password
7hash = ph.hash("correct horse battery staple")
8print(f"Hashed password: {hash}")
9
10# Verify the password
11# This will return True if successful, or raise VerifyMismatchError if it fails
12try:
13    ph.verify(hash, "correct horse battery staple")
14    print("Password verified successfully!")
15except Exception as e:
16    print(f"Verification failed: {e}")
17
18# Check if the hash needs re-hashing (e.g., if you updated security parameters)
19if ph.check_needs_rehash(hash):
20    print("Hash needs to be updated.")