acgranter/acgranter-symfony-hasher
Installation
composer require acgranter/acgranter-symfony-hasher
Add the service provider to config/app.php:
'providers' => [
// ...
Acgranter\SymfonyHasher\SymfonyHasherServiceProvider::class,
],
Basic Usage
Register the hasher in your AuthServiceProvider (or equivalent):
use Acgranter\SymfonyHasher\Hasher;
public function boot()
{
$this->app->bind(Hasher::class, function ($app) {
return new Hasher($app['hashing']);
});
}
First Use Case Hash a password (e.g., in a registration controller):
use Acgranter\SymfonyHasher\Hasher;
public function register(Request $request, Hasher $hasher)
{
$hashed = $hasher->hash($request->input('password'));
// Store $hashed in DB
}
SymfonyHasherServiceProvider.php (configures Symfony’s PasswordHasherInterface).Hasher.php (wrapper for Symfony’s hashing logic).config/acgranter.php if published).Password Hashing/Verification
// Hashing
$hashed = $hasher->hash('plain_password');
// Verification (e.g., in login logic)
if ($hasher->verify('plain_password', $stored_hash)) {
// Authenticate user
}
Integration with Laravel Auth
Extend Laravel’s Authenticatable trait or use middleware:
use Acgranter\SymfonyHasher\Hasher;
public function validateLogin(Request $request, Hasher $hasher)
{
$user = User::where('email', $request->email)->first();
return $hasher->verify($request->password, $user->password);
}
Custom Algorithms
Configure Symfony’s PasswordHasher to use specific algorithms (e.g., auto, bcrypt, argon2i):
$hasher = new Hasher($app['hashing'], 'argon2i');
password column with string type (adjust length based on algorithm, e.g., 255 for Argon2).Hasher in unit tests:
$mockHasher = Mockery::mock(Hasher::class);
$mockHasher->shouldReceive('verify')->andReturn(true);
// Old
Hash::make('password');
// New
$hasher->hash('password');
Algorithm Mismatch
argon2i but verifying with bcrypt fails silently.auto mode (detects algorithm from hash).Memory/CPU Limits
config/acgranter.php (if published):
'argon2' => [
'memory_cost' => 65536, // 64MB
],
Deprecation Warnings
PasswordHasher may log deprecations for older algorithms (e.g., sha256).auto or explicitly enable modern algorithms (bcrypt, argon2i).$hasher->needsRehash($stored_hash, 'plain_password');
// Returns true if the hash needs rehashing (e.g., algorithm upgrade).
try-catch to log Symfony-specific exceptions (e.g., InvalidArgumentException).Custom Hashers
Implement Symfony\Component\PasswordHasher\PasswordHasherInterface and bind it:
$this->app->bind(Hasher::class, function ($app) {
return new CustomHasher($app['hashing']);
});
Event Listeners Trigger events for hash upgrades (e.g., after algorithm changes):
event(new HashUpgradeRequired($user, $newHash));
Configuration Publish the config for customization:
php artisan vendor:publish --provider="Acgranter\SymfonyHasher\SymfonyHasherServiceProvider"
How can I help you explore Laravel packages today?