Advertisement
Python253

ascii_sha256_generator

May 17th, 2024
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: ascii_sha256_generator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script calculates SHA-256 hashes for every ASCII character and saves them to a file.
  10.  
  11. Functions:
  12.    - sha256sum(string):
  13.        Return the SHA-256 hash of a given string.
  14.    - calculate_ascii_sha256():
  15.        Calculate SHA-256 hashes for every ASCII character.
  16.    - save_sha256_to_file(sha256_dict, filename):
  17.        Save the SHA-256 dictionary to a file in JSON format.
  18.  
  19. Requirements:
  20.    - Python 3.x
  21.  
  22. Usage:
  23.    - Run the script to generate SHA-256 hashes for ASCII characters and save them to 'sum_ascii_sha256.txt'.
  24.  
  25. Additional Notes:
  26.    - This script may take some time to complete, depending on the speed of the system.
  27. """
  28.  
  29. import hashlib
  30. import json
  31.  
  32. def sha256sum(string):
  33.     """Return the SHA-256 hash of a given string."""
  34.     return hashlib.sha256(string.encode()).hexdigest()
  35.  
  36. def calculate_ascii_sha256():
  37.     """Calculate SHA-256 hashes for every ASCII character."""
  38.     ascii_sha256_dict = {}
  39.     print("Calculating SHA-256 hashes for ASCII characters. This may take some time...")
  40.     for i in range(128):  # ASCII range
  41.         char = chr(i)
  42.         sha256_hash = sha256sum(char)
  43.         ascii_sha256_dict[char] = sha256_hash
  44.     return ascii_sha256_dict
  45.  
  46. def save_sha256_to_file(sha256_dict, filename):
  47.     """Save the SHA-256 dictionary to a file in JSON format."""
  48.     with open(filename, 'w', encoding='utf-8') as outfile:
  49.         json.dump(sha256_dict, outfile, indent=4)
  50.  
  51. if __name__ == "__main__":
  52.     filename = 'sum_ascii_sha256.txt'
  53.     ascii_sha256_dict = calculate_ascii_sha256()
  54.     save_sha256_to_file(ascii_sha256_dict, filename)
  55.     print("\nSHA-256 hashes for ASCII characters saved to:", filename)
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement