Operations on Python hashes

cdecker picture cdecker · Apr 24, 2010 · Viewed 8k times · Source

I've got a rather strange problem. For a Distributed Hash Table I need to be able to do some simple math operations on MD5 hashes. These include a sum (numeric sum represented by the hash) and a modulo operation. Now I'm wondering what the best way to implement these operations is. I'm using hashlib to calculate the hashes, but since the hashes I get are then string, how do I calculate with them?

Answer

interjay picture interjay · Apr 24, 2010

You can use the hexdigest() method to get hexadecimal digits, and then convert them to a number:

>>> h = hashlib.md5('data')
>>> int(h.hexdigest(), 16)
188041611063492600696317361555123480284L

If you already have the output of digest(), you can convert it to hexadecimal digits:

>>> hexDig = ''.join('%02x' % ord(x) for x in h.digest())
>>> int(hexDig, 16)
188041611063492600696317361555123480284L

Edit:

For the second case, it's actually easier to convert using .encode('hex') or binascii.hexlify:

>>> int(h.digest().encode('hex'), 16)
188041611063492600696317361555123480284L
>>> int(binascii.hexlify(h.digest()), 16)
188041611063492600696317361555123480284L