Is there a way I can calculate a SHA256 hash in Python 2.4? (I emphasize: Python 2.4) I know how to do it in Python 2.5 but unfortunately it's not available on my server and an upgrade will not be made. I have the same problem as the guy in this question, but using Python 2.4.
3 Answers
Yes you can. With Python 2.4, there was SHA-1 module which does exactly this. See the documentation.
However, bear in mind that code importing from this module will cause DeprecationWarnings when run with newer Python.
Ok, as the requirement was tightened to be SHA-256, using the SHA-1 module in standard library isn't enough. I'd suggest checking out pycrypto, it has a SHA-256 implementation. There are also windows binary releases to match older Pythons, follow the links from Andrew Kuchlings old PyCrypto page.
-
If you're hashing passwords, I wouldn't recommend using SHA-1, ever. It's been cracked and is no considered insecure. Use of MD5 to hash passwords is also considered insecure. If you can't access the new hashing libraries in more recent versions of Python, I'd recommend making a system call to 'ssh-keygen' (on Linux) and reading in the file it creates. – MikeyE Sep 07 '16 at 09:13
-
It's a bit exaggerated to say "ever", back in 2009 when the question and answer was written SHA1 was OK. For reference, the first theoretical approach to making SHA1 hashes collide by Marc Stevens wasn't published until 2013. But yeah: know your hashes and understand the security implications of your choices. – af. Sep 30 '17 at 12:29
-
I hear you. When I said "ever", what I meant was "ever from this moment in time forward". But, I thought that was implied. You do make a good point. I think your answer was good for when it was written. Maybe it could be revised to be up with the times? – MikeyE Oct 01 '17 at 02:48
You can use the sha
module, if you want to stay compatible, you can import it like this:
try:
from hashlib import sha1
except ImportError:
from sha import sha as sha1

- 24,151
- 2
- 35
- 41
There is a backported version of hashlib at http://pypi.python.org/pypi/hashlib and I just backported the newer hmac version and put it up at http://pypi.python.org/pypi/hmac

- 2,909
- 17
- 20