From da0c3cdf41d39793c8703d289cef81acf183e1a8 Mon Sep 17 00:00:00 2001 From: Dennis thomas <52597699+denz647@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:35:18 +0530 Subject: [PATCH] Create filehashchecker.py --- Python/filehashchecker.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Python/filehashchecker.py diff --git a/Python/filehashchecker.py b/Python/filehashchecker.py new file mode 100644 index 0000000..2b38bbd --- /dev/null +++ b/Python/filehashchecker.py @@ -0,0 +1,31 @@ +import hashlib + +def calculate_hash(file_path, hash_type='md5', buffer_size=65536): + hash_function = hashlib.new(hash_type) + with open(file_path, 'rb') as file: + buffer = file.read(buffer_size) + while len(buffer) > 0: + hash_function.update(buffer) + buffer = file.read(buffer_size) + return hash_function.hexdigest() + +def validate_file_hash(file_path, expected_hash, hash_type='md5'): + try: + calculated_hash = calculate_hash(file_path, hash_type) + if calculated_hash == expected_hash: + print(f"Validation successful! The {hash_type.upper()} hash of the file matches the provided hash.") + else: + print(f"Validation failed! The {hash_type.upper()} hash of the file does not match the provided hash.") + except FileNotFoundError: + print("Error: File not found.") + except PermissionError: + print("Error: Permission denied to access the file.") + except Exception as e: + print(f"Error: {e}") + +if __name__ == "__main__": + file_path = input("Enter the path to the file: ") + expected_hash = input("Enter the expected hash value: ") + hash_type = input("Enter the hash type (md5, sha1, or sha256): ").lower() + + validate_file_hash(file_path, expected_hash, hash_type)