Advertisement
Python253

uni_sha256_generator

May 17th, 2024
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: uni_sha256_generator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script calculates SHA-256 hashes for all Unicode characters and saves them to a file.
  10.  
  11. Functions:
  12.    - sha256sum(string):
  13.        Return the SHA-256 hash of a given string.
  14.    - save_unicode_sha256(filename):
  15.        Save SHA-256 hashes for all Unicode characters to a file.
  16.  
  17. Requirements:
  18.    - Python 3.x
  19.  
  20. Usage:
  21.    - Run the script to generate SHA-256 hashes for Unicode characters and save them to 'sum_uni_sha256.txt'.
  22.  
  23. Additional Notes:
  24.    - This script may take some time to complete, depending on the speed of the system.
  25. """
  26.  
  27. import hashlib
  28. import unicodedata
  29.  
  30. def sha256sum(string):
  31.     """Return the SHA-256 hash of a given string."""
  32.     return hashlib.sha256(string.encode('utf-8', errors='ignore')).hexdigest()
  33.  
  34. def save_unicode_sha256(filename):
  35.     """Save SHA-256 hashes for all Unicode characters to a file."""
  36.     unicode_sha256_dict = {}
  37.     print("Calculating SHA-256 hashes for Unicode characters. This may take some time...")
  38.     # Iterate over all Unicode characters
  39.     for code_point in range(0x110000):
  40.         character = chr(code_point)  # Get Unicode character
  41.         name = unicodedata.name(character, f"<{code_point:04X}>")  # Get character name
  42.         sha256_hash = sha256sum(character)  # Calculate SHA-256 hash
  43.         unicode_sha256_dict[name] = sha256_hash  # Store in dictionary
  44.  
  45.     # Write the dictionary to the output file with JSON-like formatting
  46.     with open(filename, 'w', encoding='utf-8') as outfile:
  47.         for name, sha256_hash in sorted(unicode_sha256_dict.items()):
  48.             outfile.write(f'"{name}": "{sha256_hash}",\n')
  49.  
  50.     print(f"\nSHA-256 hashes for Unicode characters saved to:", filename)
  51.  
  52. if __name__ == "__main__":
  53.     output_filename = 'sum_uni_sha256.txt'
  54.     save_unicode_sha256(output_filename)
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement