Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: escape_md5_generator.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script generates MD5 sums for various escape sequences in Python and saves them to a file.
- Escape Sequences:
- \': Single quote
- \": Double quote
- \\: Backslash
- \n: Newline
- \r: Carriage Return
- \t: Horizontal Tab
- \b: Backspace
- \f: Formfeed
- \v: Vertical Tab
- \0: Null Character
- \\N{Name}: Unicode character Database named lookup
- \\xhh: Character with hex value hh
- Requirements:
- - Python 3.x
- Usage:
- To use this script, simply run it in a Python environment.
- Ensure Python 3.x is installed on your system.
- # Expected Results:
- # {
- # "'": {
- # "meaning": "Single quote",
- # "md5_sum": "3590cb8af0bbb9e78c343b52b93773c9"
- # },
- # "\"": {
- # "meaning": "Double quote",
- # "md5_sum": "b15835f133ff2e27c7cb28117bfae8f4"
- # },
- # "\\": {
- # "meaning": "Backslash",
- # "md5_sum": "28d397e87306b8631f3ed80d858d35f0"
- # },
- # "\n": {
- # "meaning": "Newline",
- # "md5_sum": "68b329da9893e34099c7d8ad5cb9c940"
- # },
- # "\r": {
- # "meaning": "Carriage Return",
- # "md5_sum": "dcb9be2f604e5df91deb9659bed4748d"
- # },
- # "\t": {
- # "meaning": "Horizontal Tab",
- # "md5_sum": "5e732a1878be2342dbfeff5fe3ca5aa3"
- # },
- # "\b": {
- # "meaning": "Backspace",
- # "md5_sum": "e2ba905bf306f46faca223d3cb20e2cf"
- # },
- # "\f": {
- # "meaning": "Formfeed",
- # "md5_sum": "58c89562f58fd276f592420068db8c09"
- # },
- # "\v": {
- # "meaning": "Vertical Tab",
- # "md5_sum": "13c8ffd977013703a701cf8e11deac65"
- # }
- # }
- Additional Notes:
- - The script calculates MD5 sums for each escape sequence listed in the ESCAPE_SEQUENCES dictionary.
- - The resulting MD5 sums, along with their meanings, are saved to a file named 'sum_escape.txt'.
- - The output file is formatted in JSON for easy readability and parsing.
- """
- import hashlib
- def md5sum(string):
- """Return the MD5 sum of a given string."""
- return hashlib.md5(string.encode()).hexdigest()
- def generate_escape_md5():
- """Generate MD5 sums for various escape sequences."""
- escape_md5_dict = {}
- for escape_sequence, meaning in ESCAPE_SEQUENCES.items():
- md5_hash = md5sum(escape_sequence)
- escape_md5_dict[escape_sequence] = {
- "meaning": meaning,
- "md5_sum": md5_hash
- }
- return escape_md5_dict
- def save_md5_to_file(md5_dict, filename):
- """Save the MD5 dictionary to a file in JSON format."""
- with open(filename, 'w', encoding='utf-8') as outfile:
- outfile.write("{\n")
- for escape_sequence, data in md5_dict.items():
- meaning = data["meaning"]
- md5_hash = data["md5_sum"]
- outfile.write(f' "{escape_sequence}": {{\n')
- outfile.write(f' "meaning": "{meaning}",\n')
- outfile.write(f' "md5_sum": "{md5_hash}"\n')
- outfile.write(" },\n")
- outfile.write("}\n")
- if __name__ == "__main__":
- ESCAPE_SEQUENCES = {
- "\'": "Single quote",
- "\"": "Double quote",
- "\\": "Backslash",
- "\n": "Newline",
- "\r": "Carriage Return",
- "\t": "Horizontal Tab",
- "\b": "Backspace",
- "\f": "Formfeed",
- "\v": "Vertical Tab",
- "\0": "Null Character",
- "\\N{Name}": "Unicode character Database named lookup",
- "\\xhh": "Character with hex value hh"
- }
- output_filename = 'sum_escape.txt'
- escape_md5_dict = generate_escape_md5()
- save_md5_to_file(escape_md5_dict, output_filename)
- print(f"MD5 sums for escape sequences saved to:", output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement