Understanding Argon2 in PHP Password Hashing
PHP 7.2 introduced a significant shift in password security by adding support for the Argon2 algorithm, which won the Password Hashing Competition in 2015. If you are maintaining legacy code or upgrading from older versions, understanding how to integrate this new standard without breaking existing logic is critical. The confusion often stems from how PASSWORD_DEFAULT behaves and whether your current bcrypt hashes are suddenly compromised.
Does PASSWORD_DEFAULT Automatically Use Argon2?
The short answer is no, not immediately upon upgrading PHP 7.2. While the function was added to support Argon2, the default algorithm remains whatever was defined in the version of PHP you were previously running or what your application environment dictates at runtime.
In many standard distributions, PASSWORD_DEFAULT continued to point to bcrypt for several patch cycles after the initial release. However, as of PHP 7.4, the default hash algorithm officially changed to Argon2id. If you are on PHP 7.2, 7.3, or early 7.4 builds, using PASSWORD_DEFAULT with password_hash() will still generate a bcrypt hash unless your specific build configuration explicitly set it otherwise.
To force the use of Argon2 immediately, regardless of the default setting, you should explicitly specify the algorithm constant:
$hash = password_hash('somepassword', PASSWORD_ARGON2ID, ['memory_cost' => 65536, 'time_cost' => 3, 'threads' => 2]);
This approach ensures compatibility across different PHP versions and avoids relying on potentially inconsistent default configurations. Note that while PASSWORD_BCRYPT is still supported for legacy reasons, you must explicitly choose it if you intend to hash new passwords using the older method.
Migrating Existing Passwords
One of the biggest concerns when introducing a new hashing algorithm is handling existing user data. If your database currently contains bcrypt hashes (which start with $2y$, $2a$, or $2b$), you do not need to change your verification logic for those users. The password_verify() function is designed to be agnostic to the underlying algorithm; it inspects the hash string and automatically calls the correct verification routine based on the prefix.
You can safely continue using:
if (password_verify($inputPassword, $storedHash)) {
// Access granted
}
This function will return false for an invalid password and handle bcrypt hashes correctly even if your application code is set up to prefer Argon2. However, you should plan a gradual migration strategy. As new users register or reset their passwords, start storing Argon2id hashes instead of bcrypt. Over time, the ratio of secure hashes in your database will shift without requiring downtime or complex database migrations.
Is Bcrypt Considered Insecure?
The introduction of Argon2 does not render bcrypt insecure; rather, it reflects an evolution in best practices against modern hardware threats. Bcrypt is still widely used and considered secure enough for most applications, provided the cost factor is sufficiently high. However, Argon2id offers specific advantages: it is resistant to Side-Channel attacks and, crucially, performs well on GPUs and ASICs without being overly resource-intensive.
The primary pitfall of bcrypt in the current era is its memory usage relative to CPU cycles. While bcrypt is CPU-bound, Argon2id allows you to tune both time and memory costs. This makes it more scalable against brute-force attacks on hardware farms that are increasingly common for credential stuffing. If you are starting a new project or have the luxury of migrating users, Argon2id is the preferred choice. If you are stuck with large datasets of bcrypt hashes, there is no immediate need to panic; simply ensure your cost parameter remains high (e.g., 10 or higher) and plan for a phased migration when feasible.
Implementation Considerations
When switching to Argon2id, pay close attention to the memory_cost parameter. Unlike bcrypt, which has a fixed memory footprint relative to its cost factor, Argon2id allows you to specify memory in kibibytes (KiB). A value of 65536 KiB (approx. 64 MB) is often recommended for server-side verification to ensure strong resistance against parallel attacks.
Be mindful that password_hash() with Argon2id can be slower than bcrypt on older CPUs if memory constraints are tight, but on modern servers, the performance difference is negligible compared to the security benefits. Always test your hashing setup in a staging environment first to verify that login latency remains acceptable for your specific user base.