How does a hash keep a password safe
Put fruit in a blender and you get juice. Looking only at the juice, you cannot put back exactly which fruit went in, or how much. A security hash is similar. Feed in a value and a short fingerprint comes out, but there is no way to work backward from the fingerprint to the original value. That is why services do not store your password as it is; they store only this fingerprint.
It goes one way only
There is a kind of calculation that takes any value and returns a short fingerprint of fixed length. It is called a security hash. The same value always gives the same fingerprint, and changing even one character of the input gives a completely different fingerprint.
The most important property is direction. Making a fingerprint from an input is easy, but there is no way to calculate the original input back from the fingerprint. Fruit becomes juice, but juice never turns back into fruit. That is why it is called a one-way function.
Use the make-fingerprint button to turn the input into a fingerprint. Then press reverse and the path is blocked.
The forward path opened easily, but the backward path was blocked. That block is the heart of a security hash, because the fingerprint alone does not let you work out the original value.
The most common place this property is used is passwords. If passwords are stored as they are, anyone who looks into the storage learns them at once. So a different approach is used.
Store the fingerprint instead of the password
Storing a password letter for letter is called plaintext storage. Then if the storage is breached, or someone simply looks in, every user's password is exposed. Because many people reuse the same password in several places, the damage spreads to other services too.
So the password is put through the hash and only the resulting fingerprint is stored. The storage then holds nothing but rows of fingerprints that cannot be reversed.
Press the plaintext and fingerprint buttons to compare. Plaintext storage shows the password as it is; fingerprint storage leaves only a fingerprint that cannot be reversed.
The plaintext side showed the password in the open, while the fingerprint side left only an unreadable fingerprint. Even so, a fingerprint is not a reason to relax completely. An attacker cannot calculate the fingerprint backward, but can feed common passwords into the hash one by one and see whether any produces the same fingerprint.
That is why hashes made for passwords are deliberately slow to compute, to make this kind of guessing hard. But if the password is not stored, how do we check it at login?
Verify by turning the input into a fingerprint
We use the property that the same value always gives the same fingerprint. When a user types a password at login, the server puts that input through the hash again to make a new fingerprint, then compares it with the stored fingerprint.
If the two match, the same password was entered and the user is let through; if they differ, the user is blocked. The check compares fingerprints only, with no need to look at the original password.
Pick an input and press verify. If the input's fingerprint matches the stored one, you pass; if not, you are blocked.
Only the right password gave the same fingerprint and passed. A password that differed by a single digit produced a completely different fingerprint and was blocked. Store only the fingerprint, verify by comparing fingerprints: that is how right and wrong are told apart without the original.
Hashes are used beyond passwords too. Comparing the fingerprint of a downloaded file with the published fingerprint shows whether the file changed on the way. It is a means of protecting integrity, which we met in the last lesson.
A different purpose from the hash in the Data area
The word hash also came up in the Data area. There it turned a value into a slot number so you could find the data you wanted quickly. The aim was to assign places fast and evenly, and it was no big problem if two values occasionally landed in the same slot.
A security hash may look similar, but its purpose is different. It must be impossible to go from the fingerprint back to the original value, and practically impossible to find two different inputs that give the same fingerprint.
Tap each side to compare the purposes of the two hashes. The hash in the Data area is for finding things fast; the security hash is for being impossible to reverse.
Both are called hashes, but the Data area uses one to find things fast, while security uses one so that it cannot be reversed. The purpose decides which properties the tool needs.
So even tools with the same name need different properties depending on where they are used. In security, being impossible to reverse is the most valuable property. Now let's go over everything from start to finish.
Wrapping up
In short, a security hash turns an input into a one-way fingerprint, and there is no going back from the fingerprint to the input. So only the fingerprint is stored, not the password itself. At login the input is turned into a fingerprint again and compared with the stored one; if they match, the user passes.
Unlike the hash in the Data area, used to find things fast, this hash is used so that it cannot be reversed.
Tap the key points in order to review. (one-way fingerprint → store only the fingerprint → verify with the input's fingerprint → a different purpose from the hash in the Data area)
Now you know how to protect a password without keeping it as it is. One irreversible fingerprint makes both storage and checking safe. Since nobody holds the original, there is less secret to leak.
One weakness remains, though. The same password always gives the same fingerprint, so two people with the same password get the same fingerprint, and attackers can build lists of fingerprints for common passwords in advance. The next lesson covers salt, which closes this gap.