smokin' hash

November 29, 2007 under .NET, C++, MD5, PHP, Python

No, this post will make no mention of bottle tokes.

For the remaining freelance gig in my spare time pipeline (which hopefully is winding down), an ecommerce site, I had to create simple user management system. Obviously, I’m not going to store users’ passwords as plain text in the database, but rather as a hash – an MD5 hash to be exact. Unlike the old days where you had to implement the MD5 algorithm yourself, modern programming environments have this conveniently part of some specific standard library. From PHP, hashing a password (or any other string data) is easily accomplished like this:

$plain_text = "ChrisBellini.com";
$md5_text = md5($plain_text);
// hash is '56d1df360dce2f5025c71fa7697af642'

That hashed value is then stored in the database and I can use it to compare during logins. Easy peasy.

Sometimes, though, I’d like to verify what a hashed string really is. What does “12345” look like after passing through MD5? If you can do that in your head, then you can probably move objects with your mind. For those of us lowly creatures who can’t, though, a simple way to determine this would be nice. I’m a .NET guy in my day job, so a quick console app should suffice. Using .NET’s MD5 library with C# is trivial.

using System.Security.Cryptography;

// ...

string plain_text = "ChrisBellini.com";
UTF8Encoding objEnc = new UTF8Encoding();
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte [] arrHashBytes = md5Hasher.ComputeHash(objEnc.GetBytes(plain_text));
// hash is '56-D1-DF-36-0D-CE-2F-50-25-C7-1F-A7-69-7A-F6-42'

That would do the trick, but what if I’m on my Linux computer? I could use Mono, but still… It sure would be nice to be able to do this quickly on any operating system. Enter Python.

import hashlib
 
plain_text = "ChrisBellini.com"
md5_text = hashlib.md5(plain_text).hexdigest()
# hash is '56d1df360dce2f5025c71fa7697af642'

Pop this into Python’s interactive interpreter, Idle, and you’ve got yourself some lovely hash.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »

the python programming language and its love of "whitespace"…eeeew

November 23, 2006 under porn, Programming, Python

A couple of weeks ago, I rebuilt our desktop computer; the ol’ format and reinstall everything shuffle. I like to do it a couple of times a year; usually coinciding with when my annual anti virus subscription expires. It is, however, dreadfully boring downloading and reinstalling the latest versions of the software that I use all over again.

I’ve written a few Python scripts to automate various tasks, so I needed to get a Python interpreter installed on my fresh installation of Windows. After reinstalling a lot of software in a short amount of time, my brain switched to autopilot and I entered python.com in Firefox’s address bar. I hit ENTER and averted my gaze to the TV. When I turned back to look at the monitor, I noticed that there was some “hot oral and anal action” on my screen; two things that aren’t typically associated with Python programming, unless you’re shouting at your computer because the buggy Python script that you’re writing is a pain in the ass. I then noticed the “.com” that I mistakenly entered in the URL and promptly changed it to the proper python.org. I bet python.com gets many visitors who were originally looking for python.org. Yet I wonder if any people looking for python.com accidentally visit python.org and become convinced that writing code would put their to hands to…ahem…better use. I’m glad that porn sites were too late to register clever domain names for other scripting languages’ web sites like Perl; they could’ve had fun with that one 😉

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »

quick calc

November 2, 2006 under Firefox, Google, Hacks, Python, Ruby

Often, I need to do some quick arithmetic involving multiple operations and don’t have a good calculator handy. There’s a calculator application on my cell phone, but I’m an atrociously slow cell phone typer. Most simple calculator apps, like calc.exe in Windows, are fine for single operations like 2+2 or 67 * 1.14, but for multiple-operation expressions, those simple apps won’t cut it without needing to resort to some copy ‘n’ pasting or [gasp] manual jotting with a writing utensil.

When I’m in front of a computer and I need to quickly calculate an arithmetic expression to figure out sales tax, find averages, multiply numbers too large for my brain to deal with, etc, writing a script or a total application would be overkill. Here are the cross-platform solutions that I find myself resorting to:

Python (python.org)
Python's IDLE
Python’s interpreter is convenient. It allows you to enter any mathematical express and it will evaluate it as if it were a line in a Python script. You could even assign values to variables or other data structures, use loops, conditions, and create functions like you would in any ol’ script.

Ruby (ruby-lang.org)
Ruby's IRB
Ruby’s interpreter has nearly identical features to Python’s interpreter, except that the language is Ruby instead of Python…duh 😉

Mozilla Firefox JavaScript Error Console (mozilla.org/firefox/)
In Firefox, selecting “Error Console” from the Tools menu item will launch the JavaScript Error Console window. The Error Console is useful for debugging JavaScript in Web applications. However, much like Python’s and Ruby’s interpreter, it too can quickly evaluate arithmetic expressions. Its main shortcoming, when compared to the aforementioned Python and Ruby interpreters, is that can only evaluate one line at a time. So you cannot store data in variables, since once you click the evaluate button, your variables are destroyed.
Firefox's JavaScript Error Console

Google (google.com)
Google’s powerful searching abilities are one thing, but did you know that it can also perform calculations (Ex: (99*66)2), unit conversions (Ex: 120 kilometres in miles) and even unit+currency conversion (Ex: help the in laws figure out Canadian gas prices ).

Instacalc (instacalc.com)
I’ve just recently come across this website and haven’t explored all of the features yet. However, it looks very promising. Using AJAX to display answers as you type in your expression is a nice touch.

For Windows users, there’s also the Power Calculator that’s part of the suite of Windows XP PowerToys. I keep it bound to the Calculator key on my keyboard.

better calculators + me = happy

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 1 »