Is it an MD5 digest in this Python script?
Question
I am trying to understand this simple hashlib code in Python that has been given to me the other day on Stackoverflow:
import hashlib
m = hashlib.md5()
m.update("Nobody inspects")
m.update(" the spammish repetition here")
m.digest()
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
m.digest_size
16
m.block_size
64
print m
I thought that print m
would show me the MD5 digest of the phrase: "Nobody inspects the spammish repetition here", but as a result I got this line on my local host:
<md5 HASH object @ 01806220>
Strange, when I refreshed the page, I got another line:
<md5 HASH object @ 018062E0>
and every time when I refresh it, I get another value:
md5 HASH object @ 017F8AE0
md5 HASH object @ 01806220
md5 HASH object @ 01806360
md5 HASH object @ 01806400
md5 HASH object @ 01806220
Why is it so? I guess, what I have in each line flowing "@" is not really a digest. Then, what is it? And how can I display MD5 digest here in this code?
My python version is Python 2.5 and the framework I am currently using is webapp (I have downloaded it together with SDK from "Google App Engine")
Solution
The hashlib.hash
object doesn't implement its own __str__
, so the default to-string operation is used, which prints the class name followed by its id
(address).
(Use .hexdigest()
to get the hex MD5 string.)
OTHER TIPS
print m.hexdigest()
UPADATE: hexdigest()
gives another representation of digest()
. Every character in digest is transformed into its hex representation. You can transform it with the following function:
def digest_to_hex(chars):
res = ''
for c in chars:
res = res + '%02x' % ord(c)
return res
You can also use the generator expresion
''.join('%02x' % ord(x) for x in m.digest())
or
m.digest().encode('hex')
BTW: You can use dir(some_object)
to get a list of its elements, and help(some_object)
(in the interactive interpreter) to get more informations about it.