Spaces:
Runtime error
Runtime error
File size: 1,365 Bytes
f3997d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | """Password hashing and verification"""
import bcrypt
def hash_password(password: str) -> str:
"""
Hash a plain text password using bcrypt
Args:
password: Plain text password
Returns:
Hashed password as string
"""
# Bcrypt has a 72-byte limit, truncate if necessary
password_bytes = password.encode('utf-8')
if len(password_bytes) > 72:
password_bytes = password_bytes[:72]
# Generate salt and hash
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(password_bytes, salt)
# Return as string
return hashed.decode('utf-8')
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""
Verify a plain text password against a hashed password
Args:
plain_password: Plain text password
hashed_password: Hashed password to verify against
Returns:
True if password matches, False otherwise
"""
# Truncate to 72 bytes to match hashing behavior
password_bytes = plain_password.encode('utf-8')
if len(password_bytes) > 72:
password_bytes = password_bytes[:72]
# Convert hashed password to bytes if it's a string
if isinstance(hashed_password, str):
hashed_password = hashed_password.encode('utf-8')
return bcrypt.checkpw(password_bytes, hashed_password)
|