MD5 hash of object
clp2 at rebertia.com
Mon Jun 8 21:47:00 CEST 2009
On Mon, Jun 8, 2009 at 11:43 AM, lczancanella<lczancanella at gmail.com> wrote:
> 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
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.
More information about the Python-list