views:

89

answers:

3

Hello, 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")

+2  A: 

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.)

KennyTM
@ KennyTM: Thank You, Kenny. Can You, please, tell me what is an "object's own __str__"?
brilliant
@brilliant: The `x.__str__()` is equivalent to `str(x)`.
KennyTM
@ KennyTM: I see, thank You, Kenny!!!
brilliant
@KennyTM, to be more precise, `x.__str__()` is what `str(x)` will call to get its result. This stuff is all well covered in the docs: http://docs.python.org/reference/datamodel.html?highlight=__str__#object.__str__
Peter Hansen
@ Thanks, Peter Hansen, for this comment and for the link!!!
brilliant
+2  A: 

print m.hexdigest()

Rudi
@ Rudi: Rudi, thank You very much!!! Can you, please, tell me what's the difference between digest and hexdigest?
brilliant
hexdigest() gives another representation of digist(). Every character in digest is transformed into it's 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()])
Rudi
I repost the comment as new awnser, because I can't get the code to be correct displayed in a comment.
Rudi
+1  A: 

(This is in reply to my first awnser since i can't add a comment with code)

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.

Rudi
you can edit your previous answer
SilentGhost
@ Rudi: WOW, Rudi!!!!! Thank You soooo much! I really learned a lot from this. "dir(some_object)" looks to be very useful!
brilliant