From 4d329fa82731179a7e29f2294cb1730fa1b444c3 Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 31 Oct 2026 11:33:52 +0000 Subject: [PATCH] spark: add file_hashes.py utility for SHA256/MD5 checksums --- wizard-work/spark/file_hashes.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 wizard-work/spark/file_hashes.py diff --git a/wizard-work/spark/file_hashes.py b/wizard-work/spark/file_hashes.py new file mode 100644 index 0000000..9f2215b --- /dev/null +++ b/wizard-work/spark/file_hashes.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Calculate SHA256 and MD5 hashes for files. Utility snippet.""" + +import hashlib +import sys +from pathlib import Path + + +def sha256_hash(filepath: str) -> str: + """Return SHA256 hash of file contents.""" + h = hashlib.sha256() + with open(filepath, 'rb') as f: + for chunk in iter(lambda: f.read(8192), b''): + h.update(chunk) + return h.hexdigest() + + +def md5_hash(filepath: str) -> str: + """Return MD5 hash of file contents.""" + h = hashlib.md5() + with open(filepath, 'rb') as f: + for chunk in iter(lambda: f.read(8192), b''): + h.update(chunk) + return h.hexdigest() + + +def main(): + if len(sys.argv) < 2: + print("Usage: file_hashes.py [file2...]") + sys.exit(1) + + for path in sys.argv[1:]: + p = Path(path) + if not p.exists(): + print(f"File not found: {p}") + continue + print(f"{p}") + print(f" SHA256: {sha256_hash(path)}") + print(f" MD5: {md5_hash(path)}") + + +if __name__ == "__main__": + main() \ No newline at end of file