misc.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 calculate_sha256_string(string):
  20. # Create a new SHA-256 hash object
  21. sha256_hash = hashlib.sha256()
  22. # Update the hash object with the bytes of the input string
  23. sha256_hash.update(string.encode("utf-8"))
  24. # Get the hexadecimal representation of the hash
  25. hashed_string = sha256_hash.hexdigest()
  26. return hashed_string
  27. def validate_email_format(email: str) -> bool:
  28. if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
  29. return False
  30. return True