#!/usr/bin/python # Copyright (C) 2004 Dan Carpenter # This code is released under the GNU General Public License (GPL) import sys import os import md5 # get the filename if len(sys.argv) != 2: print "Usage: %s " %(sys.argv[0]) sys.exit(1) fname = sys.argv[1] if not os.access(fname, os.R_OK): print "Error: Cannot open %s for reading" %(fname) sys.exit(1) f = file(fname) hashes = [[]] # hash all the 4k chunks # empty files should still generate valid signatures. txt = f.read(4096) hashes[0].append(md5.md5(txt).digest()) while 1: txt = f.read(4096) if not len(txt): break hashes[0].append(md5.md5(txt).digest()) # calculate all the remaining hashes j = 0 while len(hashes[j]) > 1: hashes.append([]) i = 0 while i < len(hashes[j]): tmp = hashes[j][i:i + 2] txt = "" for t in tmp: txt += t hashes[j + 1].append(md5.md5(txt).digest()) i += 2 j += 1 # change the final hash to hex for printing output = "" for byte in hashes[-1][0]: output += "%x" %(ord(byte)) print "%s" %(output)