I am trying to create an authentication mechanism using the crypt funtion.
In the php.net it says the following example:
<?php
$hashed_password = crypt('mypassword'); // let the salt be automatically generated
if (crypt($user_input, $hashed_password) == $hashed_password) {
echo "Password verified!";
}
?>
My assumption that you will generate the hashed password in the first command, and later you retrieve it from a db and use the second command to verify it, but that doesn't work, but furthermore even the script as above did not work with me as every time the crypt is running it generates a different hash because it is using a randomly generated salt.
So instead I found this script is working with me:
<?php
$hashed_password = crypt('mypassword',$salt); // Use the same salt always
if (crypt($user_input, $salt) == $hashed_password) {
echo "Password verified!";
}
?>
My question is me understanding is correct? or I am misusing the function which might cause a security hole in my mechanism?
You seem in your initial part rehashing presumably the stored hashed password which is the reason it did not work.
The other option is to rely on the encryption mechanisms that exist in the database server.
The second option means the password is stored in plain text within the database.
To illustrate the first option. During account creation/password changes
You would hash the password and then write the result into the database.
When a user attempts to login, you would retrieve the hashed password from the database and then within php and your comparison will be to compare $database_hashed_password == crypt($userprovidedpasswor
If you use mysql you can validate via a single select
I.e. If a record is returned the info provided was correct. ( you use the mysql_real_escape_string()
select * from list_users where username=$username and password=PASSWORD($user_pr
http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html