Advertisement
Python253

sum_md5_thing

May 17th, 2024 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: sum_md5_thing.py
  4. # Version: 1.0.3
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script reads an input file, calculates the MD5 sum of each line, and writes the results to an output file.
  10.  
  11. Functions:
  12.    - md5sum(string):
  13.        Returns the MD5 sum of a given string.
  14.    - process_file(local_input_filename, local_output_filename):
  15.        Reads an input file, calculates MD5 sum of each line, and writes results to an output file.
  16.  
  17. Requirements:
  18.    - Python 3.x
  19.  
  20. Usage:
  21.    - To use this script, call the process_file function with the input and output filenames as arguments.
  22.  
  23. Additional Notes:
  24.    - Make sure the input file exists and is accessible.
  25.    - The output file will be overwritten if it already exists.
  26. """
  27.  
  28. import hashlib
  29.  
  30. def md5sum(string):
  31.     """
  32.    Return the MD5 sum of a given string.
  33.  
  34.    Args:
  35.        string (str): The input string for which the MD5 sum will be calculated.
  36.  
  37.    Returns:
  38.        str: The MD5 sum of the input string.
  39.    """
  40.     return hashlib.md5(string.encode()).hexdigest()
  41.  
  42. def process_file(local_input_filename, local_output_filename):
  43.     """
  44.    Read input file, calculate MD5 sum of each line, and write results to output file.
  45.  
  46.    Args:
  47.        local_input_filename (str): The filename of the input file containing the strings.
  48.        local_output_filename (str): The filename of the output file where the MD5 sums will be saved.
  49.    """
  50.     md5_dict = {}
  51.    
  52.     print("Processing the input file. This may take some time...")
  53.    
  54.     with open(local_input_filename, 'r', encoding='utf-8') as infile:
  55.         for line in infile:
  56.             # Remove leading and trailing whitespace
  57.             line = line.strip()
  58.             # Skip empty lines
  59.             if line:
  60.                 # Compute MD5 sum
  61.                 md5_hash = md5sum(line)
  62.                 # Store the line and its MD5 hash in the dictionary
  63.                 md5_dict[line] = md5_hash
  64.    
  65.     # Write the dictionary to the output file with custom formatting
  66.     with open(local_output_filename, 'w', encoding='utf-8') as outfile:
  67.         for string, md5_hash in md5_dict.items():
  68.             outfile.write(f'"{string}": "{md5_hash}",\n')
  69.  
  70.     print("\nProcessing completed. Result saved to:", {global_output_filename}, "\n")
  71.  
  72. if __name__ == "__main__":
  73.     global_input_filename = 'input.txt'  # Replace with your list filename
  74.     global_output_filename = 'sum_md5_output.txt'
  75.     process_file(global_input_filename, global_output_filename)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement