Advertisement
Python253

escape_md5_generator

May 17th, 2024
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: escape_md5_generator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script generates MD5 sums for various escape sequences in Python and saves them to a file.
  10.  
  11. Escape Sequences:
  12.    \': Single quote
  13.    \": Double quote
  14.    \\: Backslash
  15.    \n: Newline
  16.    \r: Carriage Return
  17.    \t: Horizontal Tab
  18.    \b: Backspace
  19.    \f: Formfeed
  20.    \v: Vertical Tab
  21.    \0: Null Character
  22.    \\N{Name}: Unicode character Database named lookup
  23.    \\xhh: Character with hex value hh
  24.  
  25. Requirements:
  26.    - Python 3.x
  27.  
  28. Usage:
  29.    To use this script, simply run it in a Python environment.
  30.    Ensure Python 3.x is installed on your system.
  31.  
  32. # Expected Results:
  33. # {
  34. #     "'": {
  35. #         "meaning": "Single quote",
  36. #         "md5_sum": "3590cb8af0bbb9e78c343b52b93773c9"
  37. #     },
  38. #     "\"": {
  39. #         "meaning": "Double quote",
  40. #         "md5_sum": "b15835f133ff2e27c7cb28117bfae8f4"
  41. #     },
  42. #     "\\": {
  43. #         "meaning": "Backslash",
  44. #         "md5_sum": "28d397e87306b8631f3ed80d858d35f0"
  45. #     },
  46. #     "\n": {
  47. #         "meaning": "Newline",
  48. #         "md5_sum": "68b329da9893e34099c7d8ad5cb9c940"
  49. #     },
  50. #     "\r": {
  51. #         "meaning": "Carriage Return",
  52. #         "md5_sum": "dcb9be2f604e5df91deb9659bed4748d"
  53. #     },
  54. #     "\t": {
  55. #         "meaning": "Horizontal Tab",
  56. #         "md5_sum": "5e732a1878be2342dbfeff5fe3ca5aa3"
  57. #     },
  58. #     "\b": {
  59. #         "meaning": "Backspace",
  60. #         "md5_sum": "e2ba905bf306f46faca223d3cb20e2cf"
  61. #     },
  62. #     "\f": {
  63. #         "meaning": "Formfeed",
  64. #         "md5_sum": "58c89562f58fd276f592420068db8c09"
  65. #     },
  66. #     "\v": {
  67. #         "meaning": "Vertical Tab",
  68. #         "md5_sum": "13c8ffd977013703a701cf8e11deac65"
  69. #     }
  70. # }
  71.  
  72. Additional Notes:
  73.    - The script calculates MD5 sums for each escape sequence listed in the ESCAPE_SEQUENCES dictionary.
  74.    - The resulting MD5 sums, along with their meanings, are saved to a file named 'sum_escape.txt'.
  75.    - The output file is formatted in JSON for easy readability and parsing.
  76.  
  77. """
  78.  
  79. import hashlib
  80.  
  81. def md5sum(string):
  82.     """Return the MD5 sum of a given string."""
  83.     return hashlib.md5(string.encode()).hexdigest()
  84.  
  85. def generate_escape_md5():
  86.     """Generate MD5 sums for various escape sequences."""
  87.     escape_md5_dict = {}
  88.     for escape_sequence, meaning in ESCAPE_SEQUENCES.items():
  89.         md5_hash = md5sum(escape_sequence)
  90.         escape_md5_dict[escape_sequence] = {
  91.             "meaning": meaning,
  92.             "md5_sum": md5_hash
  93.         }
  94.     return escape_md5_dict
  95.  
  96. def save_md5_to_file(md5_dict, filename):
  97.     """Save the MD5 dictionary to a file in JSON format."""
  98.     with open(filename, 'w', encoding='utf-8') as outfile:
  99.         outfile.write("{\n")
  100.         for escape_sequence, data in md5_dict.items():
  101.             meaning = data["meaning"]
  102.             md5_hash = data["md5_sum"]
  103.             outfile.write(f'    "{escape_sequence}": {{\n')
  104.             outfile.write(f'        "meaning": "{meaning}",\n')
  105.             outfile.write(f'        "md5_sum": "{md5_hash}"\n')
  106.             outfile.write("    },\n")
  107.         outfile.write("}\n")
  108.  
  109. if __name__ == "__main__":
  110.     ESCAPE_SEQUENCES = {
  111.         "\'": "Single quote",
  112.         "\"": "Double quote",
  113.         "\\": "Backslash",
  114.         "\n": "Newline",
  115.         "\r": "Carriage Return",
  116.         "\t": "Horizontal Tab",
  117.         "\b": "Backspace",
  118.         "\f": "Formfeed",
  119.         "\v": "Vertical Tab",
  120.         "\0": "Null Character",
  121.         "\\N{Name}": "Unicode character Database named lookup",
  122.         "\\xhh": "Character with hex value hh"
  123.     }
  124.     output_filename = 'sum_escape.txt'
  125.     escape_md5_dict = generate_escape_md5()
  126.     save_md5_to_file(escape_md5_dict, output_filename)
  127.     print(f"MD5 sums for escape sequences saved to:", output_filename)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement