KDecker
KDecker

Reputation: 7148

Python compare two hex values

I am using pythons md5 library to hash my name along with a series of nonces in a Map Reduce program. I am trying to determine if the hex output of a md5 digest has at least 5 leading 0's. The md5 outputs a string 32 places in length representing the hash. I am not sure how to compare this string with a hex value.

I know I need the hex value to compare with along with a way to turn the hash output into a hex value.

I think the hex value to compare against should be 0x00000100000000000000000000000000 (thats 31 0's and a 1 in the 5th digit from the front)? But I am not sure how to represent this in python.

Also how would I turn the hex string into an actual hex value?

Here are a few I have printed out from my program so far

10ad52a892674c28ad1de4343e79c232
582d84d589a5fd57df22a8449ac70329
dda932d448b07d048f7e52be4a8234db
04e982c9890a402e248a6e5ef07c9ac4
a86bf64393f1c494a8ac3520c0abf29a

Upvotes: 1

Views: 2362

Answers (2)

jonrsharpe
jonrsharpe

Reputation: 122095

To turn the hex string into an integer value, you can use int if you specify the base:

>>> int("10ad52a892674c28ad1de4343e79c232", 16)
22167591802313671423113693555584516658

Upvotes: 0

NPE
NPE

Reputation: 500713

Why not just use string comparison, as in

if digest.hexdigest()[:5] == '0' * 5:
   # ...

where digest is the MD5 digest object.

Upvotes: 1

Related Questions