Skip to content

Instantly share code, notes, and snippets.

@dhinakg
Created October 11, 2021 03:16
Show Gist options
  • Save dhinakg/cbe30edf31ddc153fd0b0c0570c9b041 to your computer and use it in GitHub Desktop.
Save dhinakg/cbe30edf31ddc153fd0b0c0570c9b041 to your computer and use it in GitHub Desktop.
Chunklist
import argparse
import binascii
import hashlib
import struct
from pathlib import Path
from sys import exit
CHUNK_LENGTH = 4 + 32
def hexswap(i):
return struct.unpack("<I", struct.pack(">I", i))[0]
def chunk(chunklist, file_path, verbose):
chunklist = Path(chunklist).read_bytes() if isinstance(
chunklist, str) else chunklist
header = {
"magic": chunklist[:4],
"length": int.from_bytes(chunklist[4:8], "little"),
"fileVersion": chunklist[8],
"chunkMethod": chunklist[9],
"sigMethod": chunklist[10],
"chunkCount": int.from_bytes(chunklist[12:20], "little"),
"chunkOffset": int.from_bytes(chunklist[20:28], "little"),
"sigOffset": int.from_bytes(chunklist[28:36], "little")
}
all_chunks = chunklist[header["chunkOffset"]:header["chunkOffset"]+header["chunkCount"]*CHUNK_LENGTH]
chunks = [{"length": int.from_bytes(all_chunks[i:i+4], "little"), "checksum": all_chunks[i+4:i+CHUNK_LENGTH]}
for i in range(0, len(all_chunks), CHUNK_LENGTH)]
with Path(file_path).open("rb") as f:
for chunk in chunks:
status = hashlib.sha256(f.read(chunk["length"])).digest()
if not status == chunk["checksum"]:
print(
f"Chunk {chunks.index(chunk) + 1} checksum status FAIL: chunk sum {binascii.hexlify(chunk['checksum']).decode()}, calculated sum {binascii.hexlify(status).decode()}")
return False
elif verbose:
print(
f"Chunk {chunks.index(chunk) + 1} checksum status success")
return True
parser = argparse.ArgumentParser(description='Verify download.')
parser.add_argument('chunklist', type=str,
help='path')
parser.add_argument('file',
help='file to verify')
parser.add_argument('-v', '--verbose', action='store_true',
help='enable verbose printing')
args = parser.parse_args()
if chunk(args.chunklist, args.file, args.verbose):
print("No failures")
else:
print("Fail")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment