Entropy of a sha256 hash


SUBMITTED BY: Guest

DATE: March 20, 2013, 2:50 p.m.

FORMAT: Python

SIZE: 655 Bytes

HITS: 1156

  1. #!/usr/bin/env python
  2. import math
  3. import hashlib
  4. def H(data):
  5. if not data:
  6. return 0
  7. entropy = 0
  8. for x in range(len(data)):
  9. p_x = float(data.count(chr(x)))/len(data)
  10. if p_x > 0:
  11. entropy += - p_x*math.log(p_x, 2)
  12. return entropy
  13. somerandomstring = 'some random data'
  14. somerandomhash = hashlib.sha256(somerandomstring).digest()
  15. somerandomhash2 = hashlib.sha256(somerandomhash).digest()
  16. print "H(X) is the entroy function of X"
  17. print "H('%s')=%s" % (somerandomstring, H(somerandomstring))
  18. print "H(hash('%s'))=%s" % (somerandomstring,H(somerandomhash))

comments powered by Disqus