I am using md5 to hashed my password, but when I try to log in, I can't log. don't know what to do next [closed]
I have a login form with a hashed password to the database but when I log in using the password I assigned , I cannot log, I don't what to do next. I am using md5 to hashed passwords.
here is my code in inserting data to my db:
<?php
$con = mysql_connect("localhost","abc123","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_db", $con);
$password= $_POST['password'];
$encrypt_password=md5($password);
$sql="INSERT INTO username (username, password)
VALUES
('$_POST[username]','$encrypt_password')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 data added";
mysql_close($con)
?>
You have to rehash the password input the exact same way you stored it in order to validate it.
Example:
if (md5($_POST['password']) === $stored_md5_password)
{
// Password is valid
}
MD5 is usually considered a weak hashing algorithm, especially when the SHA encryptions are so easily available. Some interesting related reads:
- https://stackoverflow.com/questions/2768248/is-md5-really-that-bad
- Going from unsalted to salted MD5 passwords
- Is SHA-1 secure for password storage?
- http://www.php.net/manual/en/function.hash-hmac.php
精彩评论