Created
November 30, 2012 19:40
-
-
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.
This file contains hidden or 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
| # -*- 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