Lessons › coding and computer science · taught by Leo
Passwords and hashing: safe storage basics
Learn why passwords are hashed, how salts help, and how systems compare hashes instead of storing plain passwords.
Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.appWhy passwords should be stored safely
Passwords are like keys. You want the system to check the key, not keep the whole key lying around.
If a database gets stolen, plain text passwords are a disaster. So we store a transformed version instead.
That transformed version is usually a hash.
Hashing turns input into a fingerprint
A hash function takes data and makes a fixed-size result. Same input gives the same hash. A tiny change in the password should make a very different hash.
Think of it like a blender. Same ingredients, same smoothie. Change one ingredient, and the result changes a lot.
A hash is not encryption. Encryption is meant to be reversed with a key. Hashing is meant to be one-way.
That one-way idea is why hashes are useful for passwords.
Predict: same password, same hash?
Predict this: if the same password is entered twice, what should happen to the hash?
Choose the best idea before I show you the rule.
Remember, we want the system to compare hashes, not reveal the password.
Predict: same password, same hash?
If the same password is typed twice, what should the hash do?
- Match exactly
- Change every time
- Turn back into the password
Show answer
Match exactly: Yes. The same input should give the same hash, so the system can compare them.
Worked example: compare hashes
Here is a simple example. The user types cat one two three.
The system hashes it and stores only the hash, not the password itself.
Later, when the user logs in, the system hashes the new entry and compares the two hashes.
If they match, access is allowed.
Quick check: what is stored?
What should a secure system store for a password?
- The plain password
- The hash of the password
- A screenshot of the screen
Show answer
The hash of the password: Correct. Store the hash, because that protects the real password if the database is stolen.
Why salts stop rainbow tables
A salt is random extra data added before hashing. It makes two equal passwords produce different hashes across different users or sites.
Now the big why. If two people use the same password, a salt helps stop them from getting the same stored hash.
That makes precomputed lookup tables, called rainbow tables, much less useful.
So salt is there to add uniqueness, not secrecy.
Salt changes the hash
Picture the password and salt both feeding into the hash function.
Even if two users share the same password, different salts make different hashes.
That is the whole trick: same password, different stored result when the salt changes.
Watch out: hashing is one-way
A common mistake is mixing up hashing and encryption.
Hashing is for checking and storing fingerprints of data. Encryption is for hiding data and later getting it back.
If you need to recover the original text, hashing is the wrong tool.
Your turn: choose the safe setup
A website wants to store passwords safely. What is the best choice?
- Store plain passwords
- Store salted hashes
- Store passwords in a hidden file
Show answer
Store salted hashes: Great choice. Salted hashes are the standard safe pattern for password storage.
Recap: password safety flow
Quick checklist: first the user types the password.
Next, add a salt, then hash it.
Last, compare hashes, not plain passwords.