How Does a Login System Verify You If the Salt is Always Random?
Hello everyone !!
We build and use login systems every single day. Actually secure systems do not store our passwords in plain text in database.
So how login system works when you hit “Sign Up” and “Log In”?
Let’s break down the mechanics of password hashing and salting, too.
Phase 1: Sign Up (Storing user data securely)
Imagine you create a new account through typing username or email and password (e.g. ‘hunter’). And when you click “submit” button, the system works the following:
1. Generating Salt
The system first generates a random string, containing alpha-numeric characters, e.g. ‘x9J2k’ . This salt is private for your account.
2. Mixing
The system mixes generated salt with password you type together. (e.g. x9J2khunter)
3. Hashing
The mixed string is then run through a hashing algorithm (like SHA-256, bcrypt or Argon2). A hashing algorithm focuses on one-way method. It generates a completely scrambled, fixed-length string (also called hash) for one account and you cannot get original password from that hash.
4. Saving to database
Finally, the system stores your username or email, random salt (in this case “x9J2k”) and the resulting hash in the database.
Here, your actual password isn’t saved in database (in this case “hunter”) .
Phase 2: Log In (Proving who you are)
Later that day, you type again your username or email and password to log in. You may think like if the system generates random salt, that random salt is combined with password you type now and is run through a hashing algorithm. The resulting hash doesn’t match with one stored in database.
The system works as following:
- It finds your username or email (you type now) in database .
- If the system finds your username or email, it fetches stored salt in database and combines with the password you type now.
- The system runs this newly combined string through the exact same hashing algorithm used in Phase 1.
- The system looks at the new hash it just generates and compares it to the hash stored in the database.
- If they match, you can log in successfully.
Question is “Why do we need Salt??”
If the system only hashed the password directly without salting, attacker could use “Rainbow Table” attack, which is basically a cheat sheet containing millions of common passwords (like password123) and their pre-calculated hashes.
Because of adding random salts, if two people set the same password for their own account, it would not be same for hashes in database due to random salts. So, it makes harder for attackers to use “Rainbow Table” attack and steal accounts.
Well, I hope you will learn new thing from me ! I am happy to share with you !! Happy Hacking