Installation:
composer require ircmaxell/password-compat
Or manually include vendor/ircmaxell/password-compat/lib/password.php in your project.
Verify Compatibility:
Run the included version-test.php script to confirm your PHP environment supports the package. If it outputs "Pass", proceed.
First Use Case: Hash a password (e.g., during user registration):
$hashedPassword = password_hash('user_input_password', PASSWORD_BCRYPT);
Verify a password (e.g., during login):
if (password_verify('user_input_password', $hashedPassword)) {
// Authenticate user
}
Key Files:
vendor/ircmaxell/password-compat/lib/password.php: Core functionality.tests/: Reference implementation for edge cases.Password Hashing:
password_hash() with PASSWORD_BCRYPT (default) or PASSWORD_DEFAULT (aliases to PASSWORD_BCRYPT).options parameter:
$hashed = password_hash('password', PASSWORD_BCRYPT, ['cost' => 12]);
users.password column).Password Verification:
password_verify() for comparison:
if (password_verify($plainPassword, $storedHash)) {
// Success
}
Password Upgrading:
password_needs_rehash() to check if a hash should be upgraded (e.g., due to cost changes):
if (password_needs_rehash($storedHash, PASSWORD_BCRYPT, ['cost' => 12])) {
$newHash = password_hash($plainPassword, PASSWORD_BCRYPT, ['cost' => 12]);
// Update database
}
Integration with Laravel:
Hash facade (which internally uses password_hash):
use Illuminate\Support\Facades\Hash;
$hashed = Hash::make('password'); // Uses password_compat under the hood
Hash::check() leverages password_verify:
if (Hash::check($request->password, $user->password)) { ... }
use Illuminate\Validation\Rules\Password;
$request->validate([
'password' => ['required', new Password(min: 8, rules: Password::DEFAULT_RULES)],
]);
Migrations:
password_hash() and store the new hashes.Multi-Factor Hashing:
paragonie/sodium_compat) for hybrid hashing schemes:
$salt = random_bytes(16);
$hash = password_hash($password . $salt, PASSWORD_BCRYPT);
// Store $salt separately
Rate Limiting:
throttle middleware to prevent brute-force attacks on password verification.Audit Logging:
if (!password_verify($input, $hash)) {
Log::warning('Failed password attempt for user ' . $user->id);
}
Testing:
Hash facade in tests:
$hashed = Hash::make('password');
$this->assertTrue(Hash::check('password', $hashed));
password_verify in unit tests if needed:
$mock = Mockery::mock('alias:password_verify');
$mock->shouldReceive('verify')->andReturn(true);
Unsupported PHP Versions:
false for all password_* functions.password_compat entirely. Test with version-test.php.False Positives in Verification:
password_verify() may return true for similar but incorrect passwords due to rainbow table resistance.Password rule) and use multi-factor authentication.Cost Parameter Misuse:
cost (e.g., >15) can degrade performance.PASSWORD_BCRYPT (cost=10) unless profiling shows a need for adjustment.Legacy Hash Migration:
password_verify will fail.$user->password = password_hash($user->password, PASSWORD_BCRYPT);
$user->save();
Database Storage:
encrypt or encryptString for additional protection (though not required for password_hash).Failed Hashing:
password_hash() returns false.php -v).password.php is autoloaded (e.g., via Composer).password_hash overrides exist.Verification Issues:
password_verify() always returns false.$testHash = password_hash('test123', PASSWORD_BCRYPT);
password_verify('test123', $testHash); // Should return true
Performance Bottlenecks:
PASSWORD_BCRYPT cost=10 (default). Adjust cost if needed.remember_token).Laravel-Specific:
bcrypt (uses password_compat if needed). Override in config/hash.php:
'driver' => 'bcrypt',
'bcrypt' => [
'rounds' => 12, // Maps to `cost` in password_hash
],
Hash facade to enforce project-wide defaults:
// app/Providers/AppServiceProvider.php
Hash::extend('custom', function($app) {
return new class {
public function make($value, array $options) {
return password_hash($value, PASSWORD_BCRYPT, ['cost' => 12]);
}
public function check($value, $hashedValue, array $options) {
return password_verify($value, $hashedValue);
}
};
});
Environment-Specific Costs:
$cost = (strpos(PHP_OS, 'WIN') === false) ? 12 : 10; // Higher cost on Linux
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => $cost]);
Custom Hash Algorithms:
password_compat by adding new algorithms (e.g., Argon2 via defuse/php-encryption):
// lib/extended_password.php
require 'vendor/ircmaxell/password-compat/lib/password.php';
function password_hash_argon2($password, $options) {
// Custom implementation
}
Pre-Hash Transformations:
$pepper = config('app.password_pepper');
$hashed = password_hash($password . $pepper, PASSWORD_BCRYPT);
Post-Verification Actions:
if (!password_verify($input, $hash
How can I help you explore Laravel packages today?