First of all, let me get this out there. I am not a security guru. The genesis for this post stems from a conversation I was having with some other developers while discussing web applications. This post doesn’t even begin to scratch the surface of the math involved, but rather tries to simply explain the purpose of using salted passwords in web applications.
What is a Hash and Why Are They Useful?
A hash function takes a variable length input and converts it to a fixed length output called a hash value. It is essentially a fingerprint of the input.
We are interested in one-way hash functions such as MD5 and SHA512
Demonstration Time
This demo assumes you are using a Linux or macOS terminal.
1. Generate a SHA512 hash using “Password1” as the password.
echo "Password1" | shasum -a 512
Should produce:
0c0586a4e8f7f0cb28b2d2bbfb8c4ae95e498fbdd93da07fdaea3adbaf5f80c3
6d65a5c7966b17378c36facea770ac09dc6f1976cc2d7a175aaac847530c26aa
2. Type the same command and verify that the output is identical and you can reproduce it.
3. Now, change only 1 character and verify that the new hash value is different and has no similarity to the first has value
echo "Password2" | shasum -a 512
Should produce
8a45143930ba90da61e35ea0cc8007ced28704fcd922c214e60d7cebbb1cbfbc
7349415d0bc65bac37725a1a5a7e0b3f118f73f530631702349edbc84d7900b1
Notice that simply changing 1 character produces a completely unrelated hash value.
Dictionary Attack
If a hacker stores 1,000,000 common passwords as SHA512 values and steals a hashed password file, the hacker can then compare the list of pre-computed hash values with the hash values in the stolen password list. Any match reveals the plain text password. The hacker can then take those known passwords and try them, along with the corresponding usernames, on other sites, such as Facebook, Google, Bank sites, etc. The moral of the story is don’t use the same password on multiple sites, and as a developer, use salt when storing passwords.
Salt
Salt is a sequence of data that is randomly generated when the user creates their account and is stored along with the hashed value of the user’s plain text password combined with the salt.
Given a user’s plain text password, the server will combine that with the stored salt, and hash that value. Then the resulting hash value is compared to the stored salt+password hash. If they match, the user is granted access, otherwise the password was incorrect. The plain text password is never stored on the server.
Pseudo Code Description
When the user is created
storedSalt = Random.data
password = form.password
(Note: This password is the plain text password that the user supplied in a Login form)
storedPassword = SHA512(storedSalt + password)
And then, when the user logs in again
storedSalt = database.storedSalt
storedPasswordHash = database.storedHash
password = form.password
(Note: Again, this is the plain text password that the user supplied in the Login form)
if SHA512(storedSalt + password) == storedPassword {
// Grant access
} else {
// Incorrect password
}
Since the salt is randomly generated for each user, even users using the same plain text password on the same system will have different storedPasswordHash
values.
Now even if the hacker gets the hashed passwords, they won’t match his list of passwords, providing a degree of protection against a simple dictionary attack.
Rounds
Rounds of hashing are used to add protection against brute force attacks by adding computation time to the hash function. Hashing many times, or rounds, (250,000 or greater) adds computation time to the authentication process. Therefore, instead of computing 1,000,000 hashes per second, they only compute 4 or 5. That way it will take 200,000 times longer to crack a password list.
A Note on HTTPS
HTTPS is a protocol to abstract away the complexity of exchanging a session key between the client web browser and the HTTP server so that the data is encrypted when traveling over the internet. This is important in the context of passwords and salt because if you are passing data between the browser and the server over HTTP, vice HTTPS, the password is being passed in the clear and the rest of the security is compromised.
Leave a Reply