Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: sum_sha512_thing.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- Description:
- - This script reads an input file, calculates the SHA-512 hash of each line, and writes the results to an output file.
- Functions:
- - sha512sum(string):
- Returns the SHA-512 hash of a given string.
- - process_file(local_input_filename, local_output_filename):
- Reads an input file, calculates SHA-512 hash of each line, and writes results to an output file.
- Requirements:
- - Python 3.x
- Usage:
- - To use this script, call the process_file function with the input and output filenames as arguments.
- Additional Notes:
- - Make sure the input file exists and is accessible.
- - The output file will be overwritten if it already exists.
- """
- import hashlib
- def sha512sum(string):
- """
- Return the SHA-512 hash of a given string.
- Args:
- string (str): The input string for which the SHA-512 hash will be calculated.
- Returns:
- str: The SHA-512 hash of the input string.
- """
- return hashlib.sha512(string.encode()).hexdigest()
- def process_file(local_input_filename, local_output_filename):
- """
- Read input file, calculate SHA-512 hash of each line, and write results to output file.
- Args:
- local_input_filename (str): The filename of the input file containing the strings.
- local_output_filename (str): The filename of the output file where the SHA-512 hashes will be saved.
- """
- sha512_dict = {}
- print("Processing the input file. This may take some time...")
- with open(local_input_filename, 'r', encoding='utf-8') as infile:
- for line in infile:
- # Remove leading and trailing whitespace
- line = line.strip()
- # Skip empty lines
- if line:
- # Compute SHA-512 hash
- sha512_hash = sha512sum(line)
- # Store the line and its SHA-512 hash in the dictionary
- sha512_dict[line] = sha512_hash
- # Write the dictionary to the output file with custom formatting
- with open(local_output_filename, 'w', encoding='utf-8') as outfile:
- for string, sha512_hash in sha512_dict.items():
- outfile.write(f'"{string}": "{sha512_hash}",\n')
- print("\nProcessing completed. Result saved to:", local_output_filename, "\n")
- if __name__ == "__main__":
- global_input_filename = 'input.txt' # Replace with your list filename
- global_output_filename = 'sum_sha512_dict.txt'
- process_file(global_input_filename, global_output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement