214
Chapter 11
I
Where to go next
This allows you to do constant-time lookups. When you want to know
the
value for a key, you can use the hash function again, and it will tell
you in O(1) time what slot to check.
In this case, you want the hash function to give you a good distribution.
So a hash function takes a string and gives you
back the slot number for
that string.
Comparing files
Another hash function is a secure hash algorithm (SHA) function.
Given a string, SHA gives you a hash for that string.
The terminology can be a little confusing here. SHA is a
hash function
.
It generates a
hash
, which is just a short string.
The hash function for
hash tables went from string to array index, whereas SHA goes from
string to string.
SHA generates a different hash for every string.
Note
SHA hashes are long. They’ve been truncated here.
You can use SHA to tell whether two files are the same. This is useful
when you have very large files. Suppose you have a 4 GB file. You want
to check whether your friend has the same large file. You don’t have to
try to email them your large file. Instead,
you can both calculate the
SHA hash and compare it.
215
The SHA algorithms
Checking passwords
SHA is also useful when you want to compare strings without revealing
what the original string was. For example,
suppose Gmail gets hacked,
and the attacker steals all the passwords! Is your password out in the
open? No, it isn’t. Google doesn’t store the original password, only the
SHA hash of the password!
When you type in your password, Google
hashes it and checks it against the hash in its database.
So it’s only comparing hashes—it doesn’t have to store your password!
SHA is used very commonly to hash passwords like this. It’s a one-way
hash. You can get the hash of a string.