Hash functions are a tool from cryptography, and they are functions in the mathematical sense: for any given "acceptable" input, it spits out a specific output. In this case, the domain of acceptable inputs would be plaintext strings, and outputs are garbled-up (i.e., encrypted) versions of the plaintext strings, called hashes.
Let's play around with some examples before we go on, just so you can see what I'm talking about. Go to your favorite search engine and type "online hash function calculator." (Obviously you can use command line tools and programming APIs to compute hash functions as well.) You should see some links that allow you to calculate hashes using specific hash functions with names like MD5, SHA-1, RIPEMD-160 and others. Here's one:
Select an MD5 hash calculator, and enter some plaintext. When you hit submit, you should see a hash value. Note that whitespace in your plaintext is significant, so if you hash 'friend' and 'friend[CR]', the output will be different.
Here are some MD5 hashes:
| Plaintext input | MD5 hash (hex) |
|---|---|
friend |
3af00c6cad11f7ab5db4467b66ce503e |
friends |
28f20a02bf8a021fab4fcec48afb584e |
password |
5f4dcc3b5aa765d61d8327deb882cf99 |
I pledge allegiance to the flag |
bb00ea10b4ee04c4319c0c05bf9c29fc |
Just for kicks let's get some SHA-1 hashes for the same plaintext inputs:
| Plaintext input | SHA-1 hash (hex) |
|---|---|
friend |
e69867ca7d5a7b0ab60a2a61e7b791c106f7bf64 |
friends |
3d9209c4598bfbc38b3c096081bee3a09697e939 |
password |
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 |
I pledge allegiance to the flag |
fc1d13f4e3b942d6c31185ff033c5b7acfe22751 |
There's a lot of good stuff going on here. The main point is that MD5 and SHA-1 both do essentially the same thing: they convert plaintext into garbled text with certain properties (which we'll discuss momentarily). So they're both hash functions, albeit different hash functions.
I just mentioned "certain properties." Here are some worth noting:
The properties just given are characteristic of hash functions. For a hash function to be "cryptographically secure," we typically want two more properties to be true:
The MD5 and SHA-1 hash functions are generally considered to be secure though researchers have found weaknesses in each. For many practical purposes, it is at the time of this writing (late 2008) safe to use either of the two. If however you are doing something that requires exceptionally strong security (for example, financial applications), please consult a security expert.
That's what we needed to know about hash functions. Now let's learn how we can use them to store passwords securely.