Why md5 Is Obsolete for Passwords and What to Use Instead
Using md5() to hash passwords is a critical security vulnerability that needs immediate attention. If you are still storing user credentials using this method, your application is fundamentally insecure. The code snippet provided in your scenarioâ$pswUser = md5($_POST["password"]);âdemonstrates exactly the kind of naive implementation that leaves systems exposed to rainbow table attacks and brute force scenarios.
MD5 was designed for checksums, where data integrity matters more than security. It produces a 128-bit hash that is deterministic; the same input always yields the same output. While this might seem helpful for login sessions, it fails miserably against password storage because MD5 lacks a salt. Without a unique random value added to every password before hashing, attackers can pre-compute hashes for common passwords and instantly match them against your database. Furthermore, modern hardware makes computing MD5 hashes nearly instantaneous, rendering the "one-way" nature of the algorithm useless.
In 2017, the industry standard has shifted decisively toward functions designed specifically for password security. The most robust solution available in PHP is password_hash(). This function was introduced in PHP 5.5 and is part of the core library, meaning no external dependencies are required. Unlike MD5, password_hash() automatically handles salting using a cryptographically secure random value and stores the necessary parameters (like the algorithm version and cost factor) directly within the hash string itself.
Here is how you should implement password hashing correctly:
$hash = password_hash($_POST['password'], PASSWORD_DEFAULT);
// Store $hash in your database, never the raw password
When a user attempts to log in, you must use password_verify() to check their input against the stored hash. This function handles the complexity of verifying that the new password hashes correctly with the parameters embedded in the existing string.
if (password_verify($_POST['password'], $storedHash)) {
// Authentication successful
} else {
// Authentication failed
}
There is a significant trade-off here regarding performance, but it is a necessary one. password_hash() uses algorithms like bcrypt or Argon2 (depending on the PHP version), which are intentionally slow. This slowness acts as a defense-in-depth measure against brute-force attacks; if an attacker obtains your database, they will have to spend significantly more time guessing passwords compared to simply running MD5.
You mentioned using sessions with these hashes. While storing the hash in the session is technically redundant (since you only need the session ID and potentially a user ID), the practice of hashing passwords before comparing them is acceptable if you are migrating from an old system where you stored plain text or weak hashes. However, the goal must always be to transition away from any form of deterministic hashing like MD5, SHA1, or even SHA256 without salting.
If your codebase is built on Laravel, the framework provides a dedicated Hash facade that abstracts these details away, making it safer and more maintainable. You can simply call Hash::make($password) to create a new hash or Hash::check($plainPassword, $hashedPassword) for verification. This ensures that even if the underlying PHP version changes or you need to upgrade hashing algorithms in the future, your application remains secure without rewriting core logic.
The transition from MD5 to password_hash() is not just a recommendation; it is a requirement for any modern web application handling user data. The overhead of switching is negligible compared to the catastrophic failure mode of keeping MD5 in production.