Last active
March 3, 2021 19:07
-
-
Save zachmullen/9eec1b9a0b4a3016a3ef18272aa2b040 to your computer and use it in GitHub Desktop.
Showing computing the tree hash on
This file contains 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
from hashlib import sha256 | |
from typing import BinaryIO | |
CHUNK_LEN = 512 << 20 # 512 MB | |
def chunked_hash(data: BinaryIO, chunk_len: int = CHUNK_LEN) -> sha256: | |
checksum = sha256() | |
offset = 0 | |
while True: | |
chunk = data.read(chunk_len) | |
if not chunk: | |
break | |
chunk_checksum = sha256(chunk).hexdigest() | |
checksum.update(f'{offset} {offset + len(chunk)} {chunk_checksum}\n'.encode()) | |
offset += len(chunk) | |
return checksum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment