Reputation: 69
I know it is bad to store password as plain text in DB, because if hackers gain access to the server's DB, all usernames and passwords will be completely exposed? Therefore, original passwords are passed through hash functions, afterward they are stored in DB as series of incomprehensible characters of the same length. That is a good thing for security.
But...how can the server verify if the users enter correct passwords or not? Since the users enter their passwords in their original forms (eg: whoami, ilovecomputer...), but the server store them under "hashed" forms (eg: 234203409803249580980gfdg41cdvd4, jknegnergiuhiuhdni4584234dfgbn4j....). How can the user-entered password and the sever-stored password be matched?
Upvotes: 0
Views: 574
Reputation: 300
Here is how you authenticate users without storing their password in clear text.
You will need TWO database columns to do this. Column 1 - Password Hash, Column 2 - Salt
Column 2 - Salt is a randomly generated value that your code/system generates for each user and it is random without it ever being exposed to any UI or backend system. (I'll explain the use below)
Column 1 - Will be the HASHED value of the user's password + (concatenate) salt.
You can read more about hashing here Common Hashing Algorithms
How this whole thing works:
Hashing: Hashing is a way to create a UNIQUE value for each string based on an algorithm. The uniqueness varies based on the algorithm but unless you have a gazillion records, you should be fine. Also, the HASH value of each string is unique. Meaning the Hash of a string "test" will always be the same, lets say "123"
Salt: Salt is just a random string that is added to each password. This way the HASH value is calculated as user's PASSWORD + SALT. This ensures that even if multiple users use the same password, their Password Hash (HASH(Password+Salt)) will be unique.
Setting the password
Adding SALT is a counter measure so if two users use the same password, the HASH value will still be different.
Validating User When the user enters their username and password, this is how your code will execute.
Check if the username exists.
Upvotes: 0
Reputation: 5636
Let H
be a password hashing function. One simple property of these functions is that they a deterministic, i.e. same input outputs the same value.
First time: When users register onto the website, they require the users' password, usually two fields. Now the password is hashed h = H(passwd)
and stored in the database for the user.
Later time: User enters the user name and password, then the server gets the h
from the database by using the user name. Hashes the currently entered password pwd
, too. if H(pwd) = h
then entered password is correct. Your server lets the user continue to the system.
The above are the basics of the password system, however, that is not enough. a little gist of the password hashing;
The Final, and most important point is to educate the user about passwords. Firstly, introduce then the dicewire or similar password generation methods, and secondly, introduce them to password managers like 1passwords.
Upvotes: 1