Hashing Passwords with bcrypt or argon2
Laravel never stores passwords in plain text. Instead, it hashes them using strong, purpose-built algorithms designed to resist brute-force and rainbow-table attacks.
Hashing on Registration
$user->password = Hash::make($request->password);
Hash::make() applies the configured hashing driver — bcrypt by default — turning the plain password into a long, irreversible hash.
bcrypt vs argon2
| Algorithm | Notes |
|---|---|
| bcrypt | Laravel's default; well-tested, widely supported |
| argon2i / argon2id | Newer, memory-hard algorithm, configurable memory and thread cost |
The driver is set in config/hashing.php and can be switched without changing how the rest of the application calls Hash::make().
Verifying a Password
if (Hash::check($request->password, $user->password)) {
// password matches
}
Hashing protects stored credentials, but an application also needs to guard the data flowing in and out of every request — which brings up XSS and SQL injection.
Configuring the Hashing Driver
The config/hashing.php file holds the driver name and the cost settings for each algorithm. In recent Laravel versions most of these values can also be set from the .env file, which keeps them out of the codebase.
// config/hashing.php (simplified)
return [
'driver' => env('HASH_DRIVER', 'bcrypt'),
'bcrypt' => [
'rounds' => env('BCRYPT_ROUNDS', 12),
],
'argon' => [
'memory' => 65536, // kibibytes
'threads' => 1,
'time' => 4,
],
];
A higher bcrypt rounds value, or more Argon memory and time, makes each hash slower to compute. That slowness is the point: it makes guessing passwords expensive for an attacker. Test the login speed on your real server after changing these values, because a setting that is too heavy can slow down sign-in for every user.
Argon2 needs PHP to be compiled with Argon support. If password_hash() on your server does not offer the Argon constants, stay with bcrypt.
Upgrading Old Hashes with needsRehash
When you raise the cost or switch drivers, existing users still have hashes made with the old settings. The safe way to upgrade them is at login, because that is the only moment you have the plain password.
use Illuminate\Support\Facades\Hash;
if (Hash::check($request->password, $user->password)) {
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($request->password);
$user->save();
}
// continue with login
}
Laravel's built-in authentication can perform this rehash automatically in newer releases, but understanding the pattern helps when you write a custom login flow.
Letting the Model Hash for You
Recent Laravel versions support a hashed cast on the model. With it, assigning a plain password automatically stores a hash, which removes the risk of forgetting Hash::make() in one controller.
protected function casts(): array
{
return [
'password' => 'hashed',
];
}
Do not combine this cast with a manual Hash::make() call on the same value; pick one approach so the password is hashed exactly once.
Choosing Between bcrypt and Argon2id
Both algorithms are strong choices when configured well. bcrypt is the safe default because it works on almost every PHP build and has a long track record. Argon2id is the variant generally recommended among the Argon family, because it resists both GPU guessing and certain side-channel attacks. Pick Argon2id when your hosting supports it and you can afford the memory per login; otherwise bcrypt with a sensible cost is perfectly acceptable. Whichever you choose, keep the setting consistent across all servers that share the same user table.
Mistakes to Avoid
- Using fast hashes such as MD5 or SHA-256 for passwords. They are built for speed, which helps attackers.
- Comparing hashes with ==. Always use
Hash::check(), which handles salts and timing safely. - Logging the request body on login or registration routes, which can leak plain passwords into log files.
- Short password columns. Keep the column at
VARCHAR(255)so longer Argon hashes fit. - No rate limiting. Strong hashing works best together with login throttling and account lockout alerts.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.