Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: md5_database_verify.py
- # Version: 1.0.2
- # Author: Jeoi Reqi
- """
- Description:
- - This script allows users to verify MD5 sums against a database.
- - It loads an MD5 dictionary from a file, prompts the user to input a string & computes the MD5 sum 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:
- - md5sum(string):
- Returns the MD5 sum of a given string.
- - load_md5_database(filename):
- Loads the MD5 dictionary from a file.
- - save_md5_database(md5_dict, filename):
- Saves the MD5 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 MD5 sum, 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
- import json
- import os
- def md5sum(string):
- """
- Return the MD5 sum of a given string.
- Args:
- string (str): The input string for which the MD5 sum will be calculated.
- Returns:
- str: The MD5 sum of the input string.
- """
- return hashlib.md5(string.encode()).hexdigest()
- def load_md5_database(filename):
- """
- Load the MD5 dictionary from a file.
- Args:
- filename (str): The filename of the file containing the MD5 dictionary.
- Returns:
- dict: The loaded MD5 dictionary.
- """
- md5_dict = {}
- with open(filename, 'r', encoding='utf-8') as infile:
- for line in infile:
- # Extract string and MD5 hash from each line
- parts = line.strip().split(':', 1)
- if len(parts) == 2:
- string, md5_hash = parts
- # Remove quotes and whitespace from string
- string = string.strip().strip('"')
- # Remove quotes, whitespace, and trailing comma from MD5 hash
- md5_hash = md5_hash.strip().strip('"').strip(',')
- # Add to the dictionary
- md5_dict[string] = md5_hash
- return md5_dict
- def save_md5_database(md5_dict, filename):
- """
- Save the MD5 dictionary to a file.
- Args:
- md5_dict (dict): The MD5 dictionary to be saved.
- filename (str): The filename of the file where the MD5 dictionary will be saved.
- """
- with open(filename, 'w', encoding='utf-8') as outfile:
- for string, md5_hash in sorted(md5_dict.items()):
- outfile.write(f'"{string}": "{md5_hash}",\n')
- def main():
- # Load the MD5 dictionary
- database_filename = 'sum_md5_dict.txt'
- md5_dict = load_md5_database(database_filename)
- while True:
- # Get user input
- user_input = input("\nEnter a string to look up its MD5 sum (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 MD5 sum of the user input
- user_md5 = md5sum(user_input)
- print("\nComputed MD5 sum:")
- print(user_md5)
- # Check if the MD5 sum exists in the dictionary
- if user_input in md5_dict:
- print(f"\nThe user string '{user_input}' was found in the database.")
- print(f"\nUser string: {user_input}")
- print(f"MD5 sum: {md5_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 MD5 sum to the database
- save_option = input("\nDo you want to save this string and its MD5 sum to the database?\n1: Yes\n2: No\n\nMake your selection (1 or 2): ").strip()
- if save_option == '1':
- md5_dict[user_input] = user_md5
- save_md5_database(md5_dict, database_filename)
- print("\nString and its MD5 sum saved to the database.")
- else:
- print("\nString and its MD5 sum not saved to the database!")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement