Skip to content

Instantly share code, notes, and snippets.

@santa4nt
Created November 30, 2012 19:40
Show Gist options
  • Select an option

  • Save santa4nt/4178033 to your computer and use it in GitHub Desktop.

Select an option

Save santa4nt/4178033 to your computer and use it in GitHub Desktop.
An algorithm to calculate a chained hashing schema suitable for streamed file chunks.
# -*- coding:utf-8 -*-
"""An algorithm to calculate a hashing schema described in:
https://class.coursera.org/crypto-004/quiz/feedback?submission_id=193325
"""
import io
import sys
import hashlib
def split_file(fname, block_size=1024):
"""Open and split file fname into byte strings of length block_size.
Return a generator of such strings, with the last block having the
length in (0, block_size].
"""
with io.open(fname, mode='rb') as f:
while True:
block = f.read(block_size)
if not block:
return
yield block
f.seek(0, io.SEEK_CUR)
def calculate_hash(blocks):
# calculate the hash of the last block
h = hashlib.sha256()
h.update(blocks[-1])
hstr = h.digest()
# for the rest, append the hash of the "next" block then hash
for block in reversed(blocks[:-1]):
h = hashlib.sha256()
h.update(block)
h.update(hstr)
hstr = h.digest()
return hstr
if __name__ == '__main__':
fname = sys.argv[1]
fblocks = [block for block in split_file(fname)]
print calculate_hash(fblocks).encode('hex')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment