Passwords of the users registered with our site should be
stored as plain text, because if someone gets access to the database he will be
getting the passwords of all the users. Many
people use the same password for different websites and therefore those
accounts will be compromised as well. Therefore we need use some type of
encryption to store passwords in the database.
Simple encryption algorithms are reversible and therefore if
we use simple encryption technique to encrypt the password, the original
password can be generated from the encrypted one.
Cryptographic hashing functions such as MD5 are irreversible,
which makes it difficult to get the original password from the encrypted one.
To validate the use at login we need to again encrypt the user entered password
with MD5 and compare it with the password stored in the database at the time of
user registration. The following is the code to hash the password with MD5.
<?php
$password = 'mypassword';
$hash = md5($password);
?>
It is impossible to retrieve the original password from the hashed
passowd. If a user forgets his password, we need to simply generate a new one.
It is quite possible for someone to make a list of millions
of hashed passwords (rainbow table) and compare the hashes to find the original
passwords. Therefore the intruder can find
the original password if we use MD5. The same applies for other hashing
functions like SHA-1. This hashes are also prone to brute forcing (trying
out all combinations with an automated script).
We can use a salt to encrypt the password. A salt is
a string that is hashed together with the password and therefore one cannot get
the original password by comparing the hashed password with the rainbow tables.
The following is the code to hash the password using a salt.
<?php
$password = 'mypassword';
$salt = '12345';
$hash = md5($salt . $password);
?>
To validate the use on login we have to hash the user
entered password using the same salt and compare it with password stored in the
database.
Some additional tips to secure user accounts passwords are:
Limit the number of failed login attempts.
Minimum length of the password should be at least 8
characters or more.
Allow or even make mandatory to use special characters in
passwords.
No comments :
Post a Comment