How people stores the password in database?


I need some help with storing password in mysql database or something similar.

i used to store the password in database using md5() function but there is no way to retrieve the

password back.

Now i want to know that -
is it standard and secure way to store password?
is there any other technique to store password so i can retrive it back?

Any advice on this would be highly appreciated.



web developer
It's generally a better idea to use a one-way cipher like SHA-1. MD5 is outdated. Passwords aren't retrievable, but it's more secure to have the user reset the password if it's forgotten.

If you really have to be able to retrieve passwords, you can use a symmetrical cipher with the mcrypt library in PHP. To avoid problems, encode the ciphertext (binary output from the encryption functions) in base64 and store it in a text or varchar field in the database.



If you're using md5 or sha, you should store the output of either in the database when the user initially sets their password.


<database entry> = md5||sha( <user passwd>)
to see if their password is correct when they try to log in, you would again take the attempted password that they give you and run it through md5 or sha again and compare it to the original output of their password stored in the database


if( md5||sha( <user input> ) == get_passwd_hash_for_user( <user name> ) {
  allow login
} else {
  don't allow login
this is the basic idea behind using md5 and sha for passwords. it prevents someone from accessing all of the passwords in a system if they somehow view the database of passwords against the administrator's will. Hopefully this helps some


Notorious Olive Counter
You don't really want to be able to retrieve the password - if you can, then an attacker might be able to. It's a very standard security practice (everywhere except in Windows...).

For somewhat better security, you could use what's known as a 'salted' hash - you store a random number or a small amount of random text called the salt, a different one per user. Then you store the hash (sha-1, md5, a hashing construction of a block cipher like AES, there are a few other cryptographically sound ways) not of the password only, but of the password + the salt.

The advantage is that if an attacker recovers the entire password database, he is prevented from cracking passwords in parallel - he can't calculate the hash of one password, and compare it to every hash in the database, then calculate another hash... He has to try each password once for each user ID, since he has to included the salt individually