MD5 hash of object

Discussion in 'Python' started by lczancanella, Jun 8, 2009.

  1. lczancanella

    lczancanella Guest

    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
     
    lczancanella, Jun 8, 2009
    #1
    1. Advertising

  2. lczancanella

    Chris Rebert Guest

    On Mon, Jun 8, 2009 at 11:43 AM, lczancanella<> 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
     
    Chris Rebert, Jun 8, 2009
    #2
    1. Advertising

  3. lczancanella

    Jeff McNeil Guest

    On Jun 8, 3:47 pm, Chris Rebert <> wrote:
    > On Mon, Jun 8, 2009 at 11:43 AM, lczancanella<> 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
     
    Jeff McNeil, Jun 9, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ericko
    Replies:
    1
    Views:
    524
  2. Replies:
    9
    Views:
    17,202
    John Salerno
    May 8, 2006
  3. rp
    Replies:
    1
    Views:
    582
    red floyd
    Nov 10, 2011
  4. Pokkai Dokkai
    Replies:
    8
    Views:
    149
    Phrogz
    Nov 18, 2007
  5. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    240
    Brian Candler
    Nov 21, 2008
Loading...

Share This Page