Advertisement
Python253

sum_sha256_thing

May 17th, 2024 (edited)
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: sum_sha256_thing.py
  4. # Version: 1.0.2
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script reads an input file, calculates the SHA-256 hash of each line, and writes the results to an output file.
  10.  
  11. Functions:
  12.    - sha256sum(string):
  13.        Returns the SHA-256 hash of a given string.
  14.    - process_file(local_input_filename, local_output_filename):
  15.        Reads an input file, calculates SHA-256 hash 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 sha256sum(string):
  31.     """
  32.    Return the SHA-256 hash of a given string.
  33.  
  34.    Args:
  35.        string (str): The input string for which the SHA-256 hash will be calculated.
  36.  
  37.    Returns:
  38.        str: The SHA-256 hash of the input string.
  39.    """
  40.     return hashlib.sha256(string.encode()).hexdigest()
  41.  
  42. def process_file(local_input_filename, local_output_filename):
  43.     """
  44.    Read input file, calculate SHA-256 hash 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 SHA-256 hashes will be saved.
  49.    """
  50.     sha256_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 SHA-256 hash
  61.                 sha256_hash = sha256sum(line)
  62.                 # Store the line and its SHA-256 hash in the dictionary
  63.                 sha256_dict[line] = sha256_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, sha256_hash in sha256_dict.items():
  68.             outfile.write(f'"{string}": "{sha256_hash}",\n')
  69.  
  70.     print("\nProcessing completed. Result saved to:", local_output_filename, "\n")
  71.  
  72. if __name__ == "__main__":
  73.     global_input_filename = 'input.txt'  # Replace with your list filename
  74.     global_output_filename = 'sum_sha256_dict.txt'
  75.     process_file(global_input_filename, global_output_filename)
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement