CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 2 min read

Storing passwords

Think of a lock that stores only the shape of the key's cut pattern, and not the key. It can check a key that is put in, and a thief who opens the lock cannot copy the key from it. The analogy stops in one place: a lock's pattern can sometimes be turned into a key, while a good hash cannot be turned back.

In plain words: A system that stored passwords as text would lose all of them in one leak. Instead it stores a hash, the output of a one-way function: easy to compute from the password, hard to reverse. At login the program hashes what was typed and compares. A hash alone is still weak, because two users with the same password get the same hash, and a table of hashes of common passwords could be made once and used for all systems. A salt is a random string that is stored beside the hash and mixed into it. The same password with two salts gives two different hashes, so a table must be made again for each salt, and equal passwords cannot be spotted.

A worked example. On the test machine, /etc/shadow has one line for each user. The lines for root and nobody are:

root:*:20750:0:99999:7:::
nobody:*:20750:0:99999:7:::

The fields are the name, the password hash, the day of the last change (counted from 1970-01-01), the minimum and maximum age, the warning period, and others. Here the hash field is *, which no password can match, so these accounts cannot be used for a password login. For an account with a password, the field has the form $y$j9T$salt$hash: a prefix that names the method (y is yescrypt, 6 is SHA-512), a cost setting, the salt, and the hash. The file is readable only by root (page 2), so an ordinary user cannot copy the hashes.

The key lines. The prefix at the start of the field makes the method replaceable: old entries keep their method, and new ones can use a better one.

Salted hash

A salted hash is the output of a one-way hash function applied to a password together with a random value, the salt, which is stored beside the result. The salt makes equal passwords give different hashes and forces an attacker to attack each hash alone.

Example: the string $6$vVm32ztMQyVvVBkI$h0qS... holds the method (6), the salt (vVm32ztMQyVvVBkI) and the hash.

Check yourself: 1. Why does a system store a hash and not the password? 2. What problem does the salt solve? 3. What does * in the hash field mean?