Wednesday, August 24, 2005

More Python

We're doing some stuff at work where we need to get the Base64 encoding of the SHA-1 hash of a password. Here's how to obtain it in Python


import sha1
import base64

print base64.encodestring(sha.new('password').digest())


That's it. One line of code. No download of 3rd party. No worrying about licensing or purchasing something.

Try it in C or Java. In Java the JDK sees fit to provide neither SHA-1 or Base64. So write your own or try to download or purchase something.

In C there's RFC 3174 for SHA-1. You can try to get it to compile on your system and get it into your build. Have fun with that, I did a couple of years ago. Like Java, you'll have to thrash around the Internet for Base64 or write it yourself.

Compare all that hassle with C/Java, compared to one line in Python using built in components. Python is just better.

No comments: