hash function varies

A forum for general discussion of the Python programming language.

hash function varies

Postby 4Dummies » Sun Oct 13, 2013 4:13 am

I've had a web application working for a long time on 32-bit Linux.

I just tried moving up to a 64-bit kernel, but I'm going to have to rebuild a bunch of homebrew database stuff because the hash() function gives different results. GRRRR. :x
4Dummies
 
Posts: 2
Joined: Sun Oct 13, 2013 4:08 am

Re: hash function varies

Postby micseydel » Sun Oct 13, 2013 5:28 am

Huh?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1229
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: hash function varies

Postby casevh » Sun Oct 13, 2013 6:39 am

The return value of hash() has never been guaranteed to be stable. The return value of hash() was originally defined as a standard C long. On Unix/Linux systems, a C long is (usually) 32 bits on a 32 bit system and is (usually) 64 bits on a 64 bits. A C long is always 32 bits on Windows, even on a 64-bit version of Windows. Beginning with Python 3.2, the underlying C type was changed to also be 64 bits on 64-bit Windows. A new algorithm for calculating the hash of numeric values was also introduced in Python 3.2.

A randomization feature was added to the hashing algorithm to protect against denial-of-service attacks. The randomization is off by default in the security releases of older versions but will be enabled by default in Python 3.4. The results of hash() will change with every invocation of the Python 3.4 interpreter.

If you want a stable hash function, use the functions defined in the hashlib module.

casevh
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: hash function varies

Postby 4Dummies » Sun Oct 13, 2013 1:32 pm

Thanks for that pointer.

The instability of hash() was not mentioned in the documentation I originally used, so I looked no further. Since I have to change anyway, I'm glad to know there's a stable function to change to.
4Dummies
 
Posts: 2
Joined: Sun Oct 13, 2013 4:08 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests