Quantcast
Channel: Jaharmi’s Irreality - Python
Viewing all articles
Browse latest Browse all 15

Hashing with splash in Python

$
0
0

Every time I do hashing in Python, I have to look it up. I forget how to do it. That’s probably a bad thing, at least compared to the shell. The shell way isn’t exactly simple, but I find it something I can do by rote.

I’m going to write down how I got MD5 and SHA-1 hashes for a file — which is something I occasionally have to do when posting a download, for example — thereby making it possible to find my own perfectly-tailored how-to next time:

>>> import hashlib # hashlib is new in Python 2.5
>>> file_reference=open('/path/to/file', 'rb').read() # open the file for reading, in binary mode
>>> hashlib.md5(file_reference).hexdigest()
'11fb57ba7927ad04534d0a341dd9c943'
>>> hashlib.sha1(file_reference).hexdigest()
'bff8e8bcd74662ee52dde369e9387cb10d5a5ece'

There, that wasn’t so bad. I just have to remember the name of the built-in hashlib module and how to call for a hash of some data with it. You’re missing the twenty other lines I tried which didn’t work, of course, but you don’t really need to see that. Sigh.

Without specifying hexdigest(), the result is a hash object rather than the hash value.

>>> hashlib.md5(file_reference)
<md5 HASH object @ 0x639c0>

I compared the Python hashlib results above with the following output from OpenSSL, and they are the same:

$ openssl md5 /path/to/file
MD5(/path/to/file)= 11fb57ba7927ad04534d0a341dd9c943
$ openssl sha1 /path/to/file
SHA1(/path/to/file)= bff8e8bcd74662ee52dde369e9387cb10d5a5ece

On balance, I think I’d still like comparing that hash against another string better in Python, but getting the hashes was quite a bit more confusing to me. It was enough to interrupt my flow.


Viewing all articles
Browse latest Browse all 15

Trending Articles