Advertisement
Python253

ascii_md5_generator

May 17th, 2024
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: ascii_md5_generator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script calculates MD5 sums for every ASCII character and saves them to a file.
  10.  
  11. Functions:
  12.    - md5sum(string):
  13.        Return the MD5 sum of a given string.
  14.    - calculate_ascii_md5():
  15.        Calculate MD5 sums for every ASCII character.
  16.    - save_md5_to_file(md5_dict, filename):
  17.        Save the MD5 dictionary to a file in JSON format.
  18.  
  19. Requirements:
  20.    - Python 3.x
  21.  
  22. Usage:
  23.    - Run the script to generate MD5 sums for ASCII characters and save them to 'sum_ascii.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 md5sum(string):
  33.     """Return the MD5 sum of a given string."""
  34.     return hashlib.md5(string.encode()).hexdigest()
  35.  
  36. def calculate_ascii_md5():
  37.     """Calculate MD5 sums for every ASCII character."""
  38.     ascii_md5_dict = {}
  39.     print("Calculating MD5 sums for ASCII characters. This may take some time...")
  40.     for i in range(128):  # ASCII range
  41.         char = chr(i)
  42.         md5_hash = md5sum(char)
  43.         ascii_md5_dict[char] = md5_hash
  44.     return ascii_md5_dict
  45.  
  46. def save_md5_to_file(md5_dict, filename):
  47.     """Save the MD5 dictionary to a file in JSON format."""
  48.     with open(filename, 'w', encoding='utf-8') as outfile:
  49.         json.dump(md5_dict, outfile, indent=4)
  50.  
  51. if __name__ == "__main__":
  52.     filename = 'sum_ascii.txt'
  53.     ascii_md5_dict = calculate_ascii_md5()
  54.     save_md5_to_file(ascii_md5_dict, filename)
  55.     print("\nMD5 sums for ASCII characters saved to:", filename)
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement