#!/usr/bin/env python import math import hashlib def H(data): if not data: return 0 entropy = 0 for x in range(len(data)): p_x = float(data.count(chr(x)))/len(data) if p_x > 0: entropy += - p_x*math.log(p_x, 2) return entropy somerandomstring = 'some random data' somerandomhash = hashlib.sha256(somerandomstring).digest() somerandomhash2 = hashlib.sha256(somerandomhash).digest() print "H(X) is the entroy function of X" print "H('%s')=%s" % (somerandomstring, H(somerandomstring)) print "H(hash('%s'))=%s" % (somerandomstring,H(somerandomhash))