Last active
August 3, 2023 17:41
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
/*************************************************************************** | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Prerequisites:
How to Use:
Generated Output:
Notes:
Author: