Advertisement
Python253

escape_sha256_generator

May 17th, 2024 (edited)
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: escape_sha256_generator.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script generates SHA-256 hashes 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. #        "sha256_hash": "265fda17a34611b1533d8a281ff680dc5791b0ce0a11c25b35e11c8e75685509"
  37. #    },
  38. #    "\"": {
  39. #        "meaning": "Double quote",
  40. #        "sha256_hash": "8a331fdde7032f33a71e1b2e257d80166e348e00fcb17914f48bdb57a1c63007"
  41. #    },
  42. #    "\\": {
  43. #        "meaning": "Backslash",
  44. #        "sha256_hash": "a9253dc8529dd214e5f22397888e78d3390daa47593e26f68c18f97fd7a3876b"
  45. #    },
  46. #    "\n": {
  47. #        "meaning": "Newline",
  48. #        "sha256_hash": "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
  49. #    },
  50. #    "\r": {
  51. #        "meaning": "Carriage Return",
  52. #        "sha256_hash": "9d1e0e2d9459d06523ad13e28a4093c2316baafe7aec5b25f30eba2e113599c4"
  53. #    },
  54. #    "\t": {
  55. #        "meaning": "Horizontal Tab",
  56. #        "sha256_hash": "2b4c342f5433ebe591a1da77e013d1b72475562d48578dca8b84bac6651c3cb9"
  57. #    },
  58. #    "\b": {
  59. #        "meaning": "Backspace",
  60. #        "sha256_hash": "beead77994cf573341ec17b58bbf7eb34d2711c993c1d976b128b3188dc1829a"
  61. #    },
  62. #    "\f": {
  63. #        "meaning": "Formfeed",
  64. #        "sha256_hash": "ef6cbd2161eaea7943ce8693b9824d23d1793ffb1c0fca05b600d3899b44c977"
  65. #    },
  66. #    "\v": {
  67. #        "meaning": "Vertical Tab",
  68. #        "sha256_hash": "e7cf46a078fed4fafd0b5e3aff144802b853f8ae459a4f0c14add3314b7cc3a6"
  69. #    }
  70. # }
  71.  
  72. Additional Notes:
  73.    - The script calculates SHA-256 hashes for each escape sequence listed in the ESCAPE_SEQUENCES dictionary.
  74.    - The resulting SHA-256 hashes, along with their meanings, are saved to a file named 'sum_escape_sha256.txt'.
  75.    - The output file is formatted in JSON for easy readability and parsing.
  76.  
  77. """
  78.  
  79. import hashlib
  80.  
  81. def sha256sum(string):
  82.     """Return the SHA-256 hash of a given string."""
  83.     return hashlib.sha256(string.encode()).hexdigest()
  84.  
  85. def generate_escape_sha256():
  86.     """Generate SHA-256 hashes for various escape sequences."""
  87.     escape_sha256_dict = {}
  88.     for escape_sequence, meaning in ESCAPE_SEQUENCES.items():
  89.         sha256_hash = sha256sum(escape_sequence)
  90.         escape_sha256_dict[escape_sequence] = {
  91.             "meaning": meaning,
  92.             "sha256_hash": sha256_hash
  93.         }
  94.     return escape_sha256_dict
  95.  
  96. def save_sha256_to_file(sha256_dict, filename):
  97.     """Save the SHA-256 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 sha256_dict.items():
  101.             meaning = data["meaning"]
  102.             sha256_hash = data["sha256_hash"]
  103.             outfile.write(f'    "{escape_sequence}": {{\n')
  104.             outfile.write(f'        "meaning": "{meaning}",\n')
  105.             outfile.write(f'        "sha256_hash": "{sha256_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_sha256.txt'
  125.     escape_sha256_result = generate_escape_sha256()
  126.     save_sha256_to_file(escape_sha256_result, output_filename)
  127.     print("SHA-256 hashes for escape sequences saved to:", output_filename)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement