christian-riesen/password-hash-bundle
Installation Add the package via Composer:
composer require christian-riesen/password-hash-bundle:1.*
Ensure AppKernel.php includes the bundle:
new ChristianRiesen\PasswordHashBundle\PasswordHashBundle(),
Configure security.yml
Replace the default encoder with the custom one:
security:
encoders:
Symfony\Component\Security\Core\User\User: # Default Symfony user
id: security.encoder.passwordhash
For custom user entities (e.g., Acme\UserBundle\Entity\User):
security:
encoders:
Acme\UserBundle\Entity\User:
id: security.encoder.passwordhash
First Use Case
Use the encoder in a registration or password update form. The bundle automatically handles PHP 5.3–5.5 compatibility via password_compat.
Registration Workflow
$user = new User();
$user->setPassword($password); // Encoder auto-hashes via Symfony's UserInterface
$em->persist($user);
$em->flush();
UserInterface.Password Updates
setPassword() method):
public function setPassword($plainPassword) {
$this->password = $this->encoder->encodePassword($this, $plainPassword);
}
use Symfony\Component\DependencyInjection\ContainerInterface;
public function setPassword($plainPassword, ContainerInterface $container) {
$encoder = $container->get('security.encoder.passwordhash');
$this->password = $encoder->encodePassword($this, $plainPassword);
}
Authentication
Custom User Providers
Ensure your user entity implements Symfony\Component\Security\Core\User\UserInterface or extends Symfony\Component\Security\Core\User\AdvancedUserInterface for full compatibility.
Legacy Systems
For PHP < 5.5, the bundle falls back to password_compat. Test thoroughly to ensure compatibility with your environment.
Testing Mock the encoder in unit tests to avoid dependency on PHP’s native functions:
$encoder = $this->createMock('Symfony\Component\Security\Core\Encoder\EncoderInterface');
$encoder->expects($this->any())
->method('encodePassword')
->willReturn('hashed_password');
$this->container->set('security.encoder.passwordhash', $encoder);
PHP Version Mismatch
bcrypt).Missing UserInterface
UserInterface. Forgetting this causes silent failures.use Symfony\Component\Security\Core\User\UserInterface; and implement required methods.Overriding Encoder Configuration
security.yml syntax (e.g., missing id: security.encoder.passwordhash) breaks authentication.Password Compat Conflicts
password_compat is already loaded (e.g., via another package), conflicts may arise.ircmaxell/password_compat from other dependencies or use a custom alias.Check Encoder Service Verify the encoder is registered:
php bin/console debug:container security.encoder.passwordhash
Should return:
Service "security.encoder.passwordhash" is defined as "ChristianRiesen\PasswordHashBundle\Encoder\PasswordHashEncoder".
Log Hashing Errors Wrap password operations in try-catch blocks to log failures:
try {
$hashed = $encoder->encodePassword($user, $plainPassword);
} catch (\Exception $e) {
\Log::error("Password hash failed: " . $e->getMessage());
}
Custom Hash Algorithms
The bundle uses PHP’s native password_hash() (or password_compat). To override:
ChristianRiesen\PasswordHashBundle\Encoder\PasswordHashEncoder and inject a custom hashing strategy.Fallback Behavior Disable the fallback for PHP ≥ 5.5 by overriding the encoder service:
# app/config/services.yml
services:
security.encoder.passwordhash:
class: ChristianRiesen\PasswordHashBundle\Encoder\PasswordHashEncoder
arguments:
- "@security.password_hashers" # Use Symfony's default hashers
Legacy Password Migration To migrate old passwords (e.g., MD5) to the new format:
public function migratePassword(User $user) {
$oldHash = $user->getPassword();
$newHash = password_hash($oldHash, PASSWORD_DEFAULT); // Re-hash the old hash!
$user->setPassword($newHash);
}
How can I help you explore Laravel packages today?