fearman99's picture
Upload 64 files
447450c verified
Raw
History Blame Contribute Delete
713 Bytes
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