Skip to content

Password Hashing

A hash function takes any data, like a word or a whole file, and turns it into a short string of letters and numbers called the hash. The hash is always the same length no matter how big the input was. Think of it like a fingerprint for that data. Hashes are repeatable. Take “good morning” and run it again, and you get exactly the same hash every time on any computer. That is what lets you compare two hashes instead of comparing the data itself. Now change just one character, like adding a dot at the end, and the hash comes out completely different, nothing alike. So you cannot guess how close two inputs are by looking at their hashes.

The important part is that there is no way to turn a hash back into what went in. It only goes one direction. That is what makes a hash so useful whenever you need to check something without keeping it.

A site with a table of users could check a password by keeping each one exactly as people type them. In that case, sign-in compares what was typed with what is in the table. It works, but there is a problem. Databases leak. A backup left in the wrong place, a bug, or an attacker, and whoever gets a copy of that table reads every password in it. And since people reuse passwords, the damage goes to other sites too.

The better way is to never keep the password at all. When you pick a password, the site runs it through a hash function and stores only its hash. That stored value is called a password hash, and the password itself is thrown away. Now, at sign-in, the site hashes whatever was typed and compares the two hashes. Same password, same hash, and you are in. And if the table is stolen now, a thief gets nothing but hashes. That is why passwords are hashed and never stored.

at sign-inat sign-uppasswordhunter2hash functionone-way, fixed lengthpassword hash9f86d081…databasestores only the hashpassword discardedpassword typedhunter2hash functionsame input, same hashnew hash9f86d081…compare with stored hashmatch: signed inmismatch: denied
At sign-up only the hash is stored. At sign-in the site recomputes the hash and compares it, so a stolen database yields no passwords.