Skip to content

Instantly share code, notes, and snippets.

@ivn951
Last active August 3, 2023 17:41
Show Gist options
  • Save ivn951/552d00f69acb7404215fbdb45c333e5b to your computer and use it in GitHub Desktop.
Save ivn951/552d00f69acb7404215fbdb45c333e5b to your computer and use it in GitHub Desktop.
File Hash Calculator Script Description: The "File Hash Calculator" script is a Python utility that calculates hash values (SHA-256, SHA-512, SHA-1, and MD5)
"""
/***************************************************************************
HASH CALCULATOR
-------------------
copyright : (C) 2023 by Ivano Giuliano
email : ivano.giuliano@gmail.com
***************************************************************************/
"""
import hashlib
import os
import time
def calculate_hash(file_path, algorithm="sha256"):
if algorithm not in hashlib.algorithms_available:
raise ValueError(f"The algorithm {algorithm} is not supported.")
hash_obj = hashlib.new(algorithm)
with open(file_path, "rb") as f:
# Read the file in blocks to avoid loading the entire file into memory
for byte_block in iter(lambda: f.read(4096), b""):
hash_obj.update(byte_block)
return hash_obj.hexdigest()
def calculate_hash_for_files_in_folder(folder_path, algorithm="sha256"):
hash_dict = {}
for root, _, files in os.walk(folder_path):
for file_name in files:
file_path = os.path.join(root, file_name)
file_hash = calculate_hash(file_path, algorithm)
file_stats = os.stat(file_path)
file_size = file_stats.st_size
file_mtime = file_stats.st_mtime
file_timestamp = time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(file_mtime))
file_info = f"""
File: {file_path}
Size: {file_size} bytes
Modified Time: {file_timestamp}
{algorithm.upper()} Hash:
{file_hash}
"""
hash_dict[file_path] = file_info
return hash_dict
if __name__ == "__main__":
folder_path = input("Enter the path of the folder with the files: ")
print("Choose an algorithm for generating the hash:")
print("1. SHA-256")
print("2. SHA-512")
print("3. SHA-1")
print("4. MD5")
choice = input("Choice (1, 2, 3, or 4): ")
if choice == "1":
selected_algorithm = "sha256"
elif choice == "2":
selected_algorithm = "sha512"
elif choice == "3":
selected_algorithm = "sha1"
elif choice == "4":
selected_algorithm = "md5"
else:
print("Invalid choice.")
exit()
hash_dict = calculate_hash_for_files_in_folder(folder_path, selected_algorithm)
output_file = "file_hashes.txt"
with open(output_file, "w") as f:
for file_info in hash_dict.values():
f.write(file_info + "\n")
print(f"{selected_algorithm.upper()} hashes saved to file:", output_file)
@ivn951
Copy link
Author

ivn951 commented Aug 3, 2023

File Hash Calculator Script

Description:

The "File Hash Calculator" script is a Python utility that calculates hash values (SHA-256, SHA-512, SHA-1, and MD5) for files within a user-specified folder. Hash values, also known as fingerprints or digests, are unique representations of data that can be used to verify file integrity and detect any changes to the files.

Usage:

  1. Prerequisites:

    • Ensure you have Python 3 installed to run this script.
  2. How to Use:

    • Place the script file "file_hash_calculator.py" in the same directory as the folder containing the files you want to analyze.
    • Open a terminal or command prompt and navigate to the directory where the script is located.
    • Run the script by executing the following command:
      python file_hash_calculator.py
      
    • The script will prompt you to choose an algorithm for generating the hash value:
      • Type "1" for SHA-256
      • Type "2" for SHA-512
      • Type "3" for SHA-1
      • Type "4" for MD5
    • After selecting the algorithm, the script will process the files in the folder and calculate the hash values for each file.
    • The results will be saved to a file named "impronte_hash.html" in the same directory.
  3. Generated Output:

    • The script will create an HTML file named "impronte_hash.html" containing the information about each file and its corresponding hash value.
    • The path of each file will be displayed in blue for better visibility.
    • The file size, modification timestamp, and hash value will be presented for each file in an organized and structured manner.

Notes:

  • The script supports SHA-256, SHA-512, SHA-1, and MD5 hash algorithms.
  • It is recommended to use SHA-256 or SHA-512 for critical applications, as they provide better security.

Author:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment