password.py 833 B

1234567891011121314151617181920212223242526
  1. import base64
  2. import binascii
  3. import hashlib
  4. import re
  5. password_pattern = r"^(?=.*[a-zA-Z])(?=.*\d).{8,}$"
  6. def valid_password(password):
  7. # Define a regex pattern for password rules
  8. pattern = password_pattern
  9. # Check if the password matches the pattern
  10. if re.match(pattern, password) is not None:
  11. return password
  12. raise ValueError("Password must contain letters and numbers, and the length must be greater than 8.")
  13. def hash_password(password_str, salt_byte):
  14. dk = hashlib.pbkdf2_hmac("sha256", password_str.encode("utf-8"), salt_byte, 10000)
  15. return binascii.hexlify(dk)
  16. def compare_password(password_str, password_hashed_base64, salt_base64):
  17. # compare password for login
  18. return hash_password(password_str, base64.b64decode(salt_base64)) == base64.b64decode(password_hashed_base64)