Skip to content

Instantly share code, notes, and snippets.

@zachmullen
Last active March 3, 2021 19:07
Show Gist options
  • Save zachmullen/9eec1b9a0b4a3016a3ef18272aa2b040 to your computer and use it in GitHub Desktop.
Save zachmullen/9eec1b9a0b4a3016a3ef18272aa2b040 to your computer and use it in GitHub Desktop.
Showing computing the tree hash on
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