File size: 713 Bytes
447450c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import hashlib
import os
def hash_password(password: str) -> str:
"""Hash password using PBKDF2-HMAC-SHA256 with 100,000 iterations and a random salt."""
salt = os.urandom(16)
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
return salt.hex() + ":" + key.hex()
def verify_password(password: str, hashed: str) -> bool:
"""Verify password against a pbkdf2 hash string."""
try:
salt_hex, key_hex = hashed.split(":")
salt = bytes.fromhex(salt_hex)
key = bytes.fromhex(key_hex)
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
return key == new_key
except Exception:
return False
|