In web development, secure storage of user passwords is a fundamental and critical task. However, despite the widespread awareness of security, many developers still habitually use the md5() function to encrypt and store user passwords when starting with PHP or building simple systems. Today, let's discuss whether storing passwords with md5 is truly secure, and what common misconceptions exist behind this approach.
md5() is a built-in hash function in PHP that converts a string of any length into a 32-character hexadecimal string. For example:
<span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">md5</span></span><span>(</span><span><span class="hljs-string">'password123'</span></span><span>); </span><span><span class="hljs-comment">// Outputs: 482c811da5d5b4bc6d497ffa98491e38</span></span><span>
</span></span>
This is an irreversible hashing process (in theory). In the past, many developers used it to “encrypt” user passwords, believing it could protect user privacy.
The answer is: not safe, and it has been deprecated.
The main reasons are as follows:
md5() was originally intended as a hash function for data integrity checks, and it computes hashes extremely quickly. However, its speed makes it very susceptible to brute force attacks or rainbow table attacks.
Attackers can try a vast number of combinations in a very short time to guess the original password, and this is especially problematic for common or weak passwords, which are nearly defenseless.
md5() is a deterministic function—identical inputs will always produce the same output. This means that if two users have the same password, the generated hash will be identical, and an attacker only needs to crack it once to access multiple accounts.
A hash storage method without salt is very vulnerable to rainbow table attacks.
There are vast databases online mapping md5 hashes to plaintext passwords. For example, you can simply paste the md5('123456') hash into a search engine and often immediately find the original password.
Starting from PHP 5.5, PHP introduced the functions password_hash() and password_verify(), which are now the recommended methods for encrypting and verifying passwords.
Sample code is as follows:
<span><span><span class="hljs-comment">// Generate password hash</span></span><span>
</span><span><span class="hljs-variable">$hash</span></span><span> = </span><span><span class="hljs-title function_ invoke__">password_hash</span></span><span>(</span><span><span class="hljs-string">'password123'</span></span><span>, PASSWORD_DEFAULT);
<p></span>// Verify password<br>
if (password_verify('password123', $hash)) {<br>
echo 'Correct password';<br>
} else {<br>
echo 'Incorrect password';<br>
}<br>
</span>
password_hash() by default uses the bcrypt algorithm and automatically generates a random salt value, with built-in mechanisms to resist brute-force and rainbow table attacks.
Automatically salts passwords
Configurable computation intensity (cost factor)
Extensible (supports different algorithms like Argon2)
Officially maintained and adheres to modern password storage standards
Using md5() to store passwords is an outdated and dangerous practice. While it was common in earlier projects, it is no longer secure today. With the advancement of cyber attack techniques, developers should abandon outdated security practices and adopt more modern and secure ways of handling user passwords.
Remember: security is not about "encryption," but about design.
Embrace password_hash() and reject md5()—this is the most basic respect for user security.