Today, I asked an AI to help me make code that can calculate the size and cryptographic checksums of binary files put in the directory. It does this by opening each file in small chunks, and it processes the data to generate CRC32, MD5, and SHA‑256 hashes, and then it prints all the results in a clean format. It uses th sys module to get the file names, os to check the file size, it uses binascii to create the CRC32 value, and hashlib to make the MD5 and SHA‑256 hashes.
import sys
import os
import binascii
import hashlib
def crc32_hex(path):
crc = 0
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
crc = binascii.crc32(chunk, crc)
# Ensure unsigned 32-bit and format as 8 lowercase hex digits
return format(crc & 0xFFFFFFFF, "08x")
def md5_hex(path):
h = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
return h.hexdigest()
Leave a Reply