hashing.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import bcrypt
  2. class Hasher:
  3. @staticmethod
  4. def verify_password(plain_password: str, hashed_password: str) -> bool:
  5. if not plain_password or not hashed_password:
  6. return False
  7. try:
  8. # Direct bcrypt verification
  9. password_bytes = plain_password.encode('utf-8')
  10. # Bcrypt has a 72-byte limit. We truncate to match hashing logic.
  11. if len(password_bytes) > 71:
  12. password_bytes = password_bytes[:71]
  13. hashed_bytes = hashed_password.encode('utf-8')
  14. return bcrypt.checkpw(password_bytes, hashed_bytes)
  15. except Exception:
  16. return False
  17. @staticmethod
  18. def get_password_hash(password: str) -> str:
  19. # Direct bcrypt hashing
  20. password_bytes = password.encode('utf-8')
  21. # Bcrypt has a 72-byte limit. We truncate to 71 to be safe.
  22. if len(password_bytes) > 71:
  23. password_bytes = password_bytes[:71]
  24. salt = bcrypt.gensalt()
  25. hashed = bcrypt.hashpw(password_bytes, salt)
  26. return hashed.decode('utf-8')