What are the potential security risks of storing passwords as MD5 hashes in a database and how can they be mitigated?
Storing passwords as MD5 hashes in a database is insecure because MD5 is a fast hashing algorithm that can be easily cracked using rainbow tables or brute force attacks. To mitigate this risk, passwords should be hashed using a more secure algorithm such as bcrypt, which is specifically designed for password hashing and includes features like salting and key stretching to make it more resistant to attacks.
// Hashing a password using bcrypt
$password = "password123";
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
// Verifying a password
$entered_password = "password123";
if (password_verify($entered_password, $hashed_password)) {
echo "Password is correct";
} else {
echo "Password is incorrect";
}