Last active
October 31, 2024 13:14
-
-
Save ariten/56e802bd60e6dbca69e8329278417f32 to your computer and use it in GitHub Desktop.
Hashing a Directory with MD5 and SHA256 and outputs them in to a CSV, aim is to provide a snapshot of a directory at a moment in time.
This file contains hidden or 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
import hashlib | |
import csv | |
import os | |
import datetime | |
def calcualte_hash(file_path): | |
hash_md5 = hashlib.md5() | |
hash_SHA256 = hashlib.sha256() | |
try: | |
with open(file_path, "rb") as f: | |
while chunk := f.read(8192): | |
hash_md5.update(chunk) | |
hash_SHA256.update(chunk) | |
except PermissionError: | |
print(f"Error on {file_path}") | |
return "ERROR", "ERROR" | |
return hash_md5.hexdigest(), hash_SHA256.hexdigest() | |
def hash_dir(file_path): | |
output_csv_filename = f"{datetime.datetime.now(datetime.timezone.utc).strftime("%d-%m-%yT%H-%M-%S")}Z hash-check.csv" | |
with open(output_csv_filename, mode="w", newline="", encoding="utf-8") as csv_file: | |
fieldnames = ["Time of Hash", "MD5", "SHA256", "File Name", "Path of File"] | |
writer = csv.DictWriter(csv_file, fieldnames=fieldnames) | |
if csv_file.tell() == 0: | |
writer.writeheader() | |
for root, _, files in os.walk(file_path): | |
for file_name in files: | |
file_path = os.path.join(root, file_name) | |
if os.path.isfile(file_path): | |
md5, sha256 = calcualte_hash(file_path=file_path) | |
writer.writerow({"Time of Hash": datetime.datetime.now(datetime.timezone.utc).isoformat(), "MD5": md5, "SHA256": sha256, "File Name":file_name, "Path of File": file_path}) | |
csv_file.close() | |
extract = "" | |
hash_dir(extract) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment