misc.py 819 B

123456789101112131415161718192021222324252627282930
  1. import hashlib
  2. import re
  3. def get_gravatar_url(email):
  4. # Trim leading and trailing whitespace from
  5. # an email address and force all characters
  6. # to lower case
  7. address = str(email).strip().lower()
  8. # Create a SHA256 hash of the final string
  9. hash_object = hashlib.sha256(address.encode())
  10. hash_hex = hash_object.hexdigest()
  11. # Grab the actual image URL
  12. return f"https://www.gravatar.com/avatar/{hash_hex}?d=mp"
  13. def calculate_sha256(file):
  14. sha256 = hashlib.sha256()
  15. # Read the file in chunks to efficiently handle large files
  16. for chunk in iter(lambda: file.read(8192), b""):
  17. sha256.update(chunk)
  18. return sha256.hexdigest()
  19. def validate_email_format(email: str) -> bool:
  20. if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
  21. return False
  22. return True