|
|
|
|
|
""" |
|
|
Simple script to compare two .bin files for exact equality. |
|
|
Usage: python simple_compare.py file1.bin file2.bin |
|
|
""" |
|
|
|
|
|
import sys |
|
|
import os |
|
|
import hashlib |
|
|
|
|
|
|
|
|
def compare_bin_files(file1, file2): |
|
|
"""Compare two .bin files for exact equality""" |
|
|
|
|
|
|
|
|
if not os.path.exists(file1): |
|
|
print(f"β File 1 does not exist: {file1}") |
|
|
return False |
|
|
|
|
|
if not os.path.exists(file2): |
|
|
print(f"β File 2 does not exist: {file2}") |
|
|
return False |
|
|
|
|
|
|
|
|
size1 = os.path.getsize(file1) |
|
|
size2 = os.path.getsize(file2) |
|
|
|
|
|
print(f"File 1: {file1} ({size1:,} bytes)") |
|
|
print(f"File 2: {file2} ({size2:,} bytes)") |
|
|
|
|
|
|
|
|
if size1 != size2: |
|
|
print(f"β Files have different sizes: {size1:,} vs {size2:,}") |
|
|
return False |
|
|
|
|
|
|
|
|
print("Calculating hashes...") |
|
|
hash1 = hashlib.sha256() |
|
|
hash2 = hashlib.sha256() |
|
|
|
|
|
with open(file1, 'rb') as f1, open(file2, 'rb') as f2: |
|
|
while True: |
|
|
chunk1 = f1.read(8192) |
|
|
chunk2 = f2.read(8192) |
|
|
|
|
|
if not chunk1 and not chunk2: |
|
|
break |
|
|
|
|
|
if chunk1: |
|
|
hash1.update(chunk1) |
|
|
if chunk2: |
|
|
hash2.update(chunk2) |
|
|
|
|
|
hash1_hex = hash1.hexdigest() |
|
|
hash2_hex = hash2.hexdigest() |
|
|
|
|
|
print(f"File 1 hash: {hash1_hex}") |
|
|
print(f"File 2 hash: {hash2_hex}") |
|
|
|
|
|
if hash1_hex == hash2_hex: |
|
|
print("β
Files are identical") |
|
|
return True |
|
|
else: |
|
|
print("β Files are different") |
|
|
return False |
|
|
|
|
|
|
|
|
def main(): |
|
|
if len(sys.argv) != 3: |
|
|
print("Usage: python simple_compare.py file1.bin file2.bin") |
|
|
sys.exit(1) |
|
|
|
|
|
file1 = sys.argv[1] |
|
|
file2 = sys.argv[2] |
|
|
|
|
|
result = compare_bin_files(file1, file2) |
|
|
sys.exit(0 if result else 1) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |