Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > MD5 hash of object

Reply
Thread Tools

MD5 hash of object

 
 
lczancanella
Guest
Posts: n/a
 
      06-08-2009
Hi,

in hashlib the hash methods have as parameters just string, i want to
know how can i digest an object to get a md5 hash of them.

Thankz

Luiz
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      06-08-2009
On Mon, Jun 8, 2009 at 11:43 AM, lczancanella<(E-Mail Removed)> wrote:
> Hi,
>
> in hashlib the hash methods have as parameters just string, i want to
> know how can i digest an object to get a md5 hash of them.


Hashes are only defined to operate on bytestrings. Since Python is a
high-level language and doesn't permit you to view the internal binary
representation of objects, you're going to have to properly convert
the object to a bytestring first, a process called "serialization".
The `pickle` and `json` serialization modules are included in the
standard library. These modules can convert objects to bytestrings and
back again.
Once you've done the bytestring conversion, just run the hash method
on the bytestring.

Be careful when serializing dictionaries and sets though, because they
are arbitrarily ordered, so two dictionaries containing the same items
and which compare equal may have a different internal ordering, thus
different serializations, and thus different hashes.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Jeff McNeil
Guest
Posts: n/a
 
      06-09-2009
On Jun 8, 3:47*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Mon, Jun 8, 2009 at 11:43 AM, lczancanella<(E-Mail Removed)> wrote:
> > Hi,

>
> > in hashlib the hash methods have as parameters just string, i want to
> > know how can i digest an object to get a md5 hash of them.

>
> Hashes are only defined to operate on bytestrings. Since Python is a
> high-level language and doesn't permit you to view the internal binary
> representation of objects, you're going to have to properly convert
> the object to a bytestring first, a process called "serialization".
> The `pickle` and `json` serialization modules are included in the
> standard library. These modules can convert objects to bytestrings and
> back again.
> Once you've done the bytestring conversion, just run the hash method
> on the bytestring.
>
> Be careful when serializing dictionaries and sets though, because they
> are arbitrarily ordered, so two dictionaries containing the same items
> and which compare equal may have a different internal ordering, thus
> different serializations, and thus different hashes.
>
> Cheers,
> Chris
> --http://blog.rebertia.com


I'd think that using the hash of the pickled representation of an
object might be problematic, no? The pickle protocol handles object
graphs in a way that allows it to preserve references back to
identical objects. Consider the following (contrived) example:

import pickle
from hashlib import md5

class Value(object):
def __init__(self, v):
self._v = v

class P1(object):
def __init__(self, name):
self.name = Value(name)
self.other_name = self.name

class P2(object):
def __init__(self, name):
self.name = Value(name)
self.other_name = Value(name)

h1 = md5(pickle.dumps(P1('sabres'))).hexdigest()
h2 = md5(pickle.dumps(P2('sabres'))).hexdigest()

print h1 == h2
>>> False


Just something to be aware of. Depending on what you're trying to
accomplish, it may make sense to simply define a method which
generates a byte string representation of your object's state and just
return the hash of that value.

Thanks,

-Jeff
mcjeff.blogspot.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
create a md5 / md5 passwd with a salt Peter Woodsky Ruby 6 11-21-2008 09:08 AM
make a hash object to string object as look like a hash ? Pokkai Dokkai Ruby 8 11-18-2007 05:23 AM
md5 from python different then md5 from command line ursache.marius@gmail.com Python 9 05-07-2006 11:49 PM
I remember someone asking about an MD5 javascript: http://pajhome.org.uk/crypt/md5/ Mozzie \( v \) Javascript 0 07-12-2004 01:06 PM



Advertisments