Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: sha256_database_verify.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script allows users to verify SHA-256 hashes against a database.
- - It loads a SHA-256 dictionary from a file, prompts the user to input a string & computes the SHA-256 hash of the input string.
- - It then checks if the user input string already exists in the dictionary.
- - If the string is not found, the user has the option to save it to the database.
- Functions:
- - sha256sum(string):
- Returns the SHA-256 hash of a given string.
- - load_sha256_database(filename):
- Loads the SHA-256 dictionary from a file.
- - save_sha256_database(sha256_dict, filename):
- Saves the SHA-256 dictionary to a file.
- - main():
- Main function to execute the script.
- Requirements:
- - Python 3.x
- Usage:
- - Run the script and follow the prompts. Enter a string to look up its SHA-256 hash, or type 'exit' to quit.
- - If the string is not found in the database, you will be prompted to save it.
- Additional Notes:
- - Make sure the database file exists and is accessible.
- - The database file will be overwritten if any changes are made.
- """
- import hashlib
- def sha256sum(string):
- """
- Return the SHA-256 hash of a given string.
- Args:
- string (str): The input string for which the SHA-256 hash will be calculated.
- Returns:
- str: The SHA-256 hash of the input string.
- """
- return hashlib.sha256(string.encode()).hexdigest()
- def load_sha256_database(filename):
- """
- Load the SHA-256 dictionary from a file.
- Args:
- filename (str): The filename of the file containing the SHA-256 dictionary.
- Returns:
- dict: The loaded SHA-256 dictionary.
- """
- sha256_dict = {}
- with open(filename, 'r', encoding='utf-8') as infile:
- for line in infile:
- # Extract string and SHA-256 hash from each line
- parts = line.strip().split(':', 1)
- if len(parts) == 2:
- string, sha256_hash = parts
- # Remove quotes and whitespace from string
- string = string.strip().strip('"')
- # Remove quotes, whitespace, and trailing comma from SHA-256 hash
- sha256_hash = sha256_hash.strip().strip('"').strip(',')
- # Add to the dictionary
- sha256_dict[string] = sha256_hash
- return sha256_dict
- def save_sha256_database(sha256_dict, filename):
- """
- Save the SHA-256 dictionary to a file.
- Args:
- sha256_dict (dict): The SHA-256 dictionary to be saved.
- filename (str): The filename of the file where the SHA-256 dictionary will be saved.
- """
- with open(filename, 'w', encoding='utf-8') as outfile:
- for string, sha256_hash in sorted(sha256_dict.items()):
- outfile.write(f'"{string}": "{sha256_hash}",\n')
- def main():
- # Load the SHA-256 dictionary
- database_filename = 'sum_sha256_dict.txt'
- sha256_dict = load_sha256_database(database_filename)
- while True:
- # Get user input
- user_input = input("\nEnter a string to look up its SHA-256 hash (or type 'exit' to quit): ").strip()
- # Check if the user wants to exit
- if user_input.lower() == 'exit':
- print("\nExiting the program...\tGoodbye!\n")
- break
- # Compute the SHA-256 hash of the user input
- user_sha256 = sha256sum(user_input)
- print("\nComputed SHA-256 hash:")
- print(user_sha256)
- # Check if the SHA-256 hash exists in the dictionary
- if user_input in sha256_dict:
- print(f"\nThe user string '{user_input}' was found in the database.")
- print(f"\nUser string: {user_input}")
- print(f"SHA-256 hash: {sha256_dict[user_input]}")
- else:
- print(f"\nThe user string '{user_input}' was not found in the database.")
- # Ask the user if they want to save the string and its SHA-256 hash to the database
- save_option = input("\nDo you want to save this string and its SHA-256 hash to the database?\n1: Yes\n2: No\n\nMake your selection (1 or 2): ").strip()
- if save_option == '1':
- sha256_dict[user_input] = user_sha256
- save_sha256_database(sha256_dict, database_filename)
- print("\nString and its SHA-256 hash saved to the database.")
- else:
- print("\nString and its SHA-256 hash not saved to the database!")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement