Permalink
Browse files

Added BCrypt password encoder.

  • Loading branch information...
elnur authored and fabpot committed Dec 1, 2012
1 parent af5abfc commit 2880e303e80b9f7851e8ea4300c7e6be9b2f8320
View
@@ -5,6 +5,7 @@ CHANGELOG
-----
* Added PBKDF2 Password encoder
+* Added BCrypt password encoder
2.1.0
-----
@@ -383,6 +383,11 @@ private function addEncodersSection(ArrayNodeDefinition $rootNode)
->booleanNode('ignore_case')->defaultFalse()->end()
->booleanNode('encode_as_base64')->defaultTrue()->end()
->scalarNode('iterations')->defaultValue(5000)->end()
+ ->integerNode('cost')
+ ->min(4)
+ ->max(31)
+ ->defaultValue(13)
+ ->end()
->scalarNode('id')->end()
->end()
->end()
@@ -13,6 +13,7 @@
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
+use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -464,6 +465,19 @@ private function createEncoder($config, ContainerBuilder $container)
);
}
+ // bcrypt encoder
+ if ('bcrypt' === $config['algorithm']) {
+ $arguments = array(
+ new Reference('security.secure_random'),
+ $config['cost'],
+ );
+
+ return array(
+ 'class' => new Parameter('security.encoder.bcrypt.class'),
+ 'arguments' => $arguments,
+ );
+ }
+
// message digest encoder
$arguments = array(
$config['algorithm'],
@@ -13,6 +13,7 @@
<parameter key="security.encoder.digest.class">Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder</parameter>
<parameter key="security.encoder.plain.class">Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder</parameter>
<parameter key="security.encoder.pbkdf2.class">Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder</parameter>
+ <parameter key="security.encoder.bcrypt.class">Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder</parameter>
<parameter key="security.user.provider.in_memory.class">Symfony\Component\Security\Core\User\InMemoryUserProvider</parameter>
<parameter key="security.user.provider.in_memory.user.class">Symfony\Component\Security\Core\User\User</parameter>
@@ -22,6 +22,10 @@
'iterations' => 5,
'key_length' => 30,
),
+ 'JMS\FooBundle\Entity\User6' => array(
+ 'algorithm' => 'bcrypt',
+ 'cost' => 15,
+ ),
),
'providers' => array(
'default' => array(
@@ -18,6 +18,8 @@
<encoder class="JMS\FooBundle\Entity\User5" algorithm="pbkdf2" hash-algorithm="sha1" encode-as-base64="false" iterations="5" key-length="30" />
+ <encoder class="JMS\FooBundle\Entity\User6" algorithm="bcrypt" cost="15" />
+
<provider name="default">
<memory>
<user name="foo" password="foo" roles="ROLE_USER" />
@@ -16,6 +16,9 @@ security:
encode_as_base64: false
iterations: 5
key_length: 30
+ JMS\FooBundle\Entity\User6:
+ algorithm: bcrypt
+ cost: 15
providers:
default:
@@ -158,6 +158,13 @@ public function testEncoders()
'class' => new Parameter('security.encoder.pbkdf2.class'),
'arguments' => array('sha1', false, 5, 30),
),
+ 'JMS\FooBundle\Entity\User6' => array(
+ 'class' => new Parameter('security.encoder.bcrypt.class'),
+ 'arguments' => array(
+ new Reference('security.secure_random'),
+ 15,
+ )
+ ),
)), $container->getDefinition('security.encoder_factory.generic')->getArguments());
}

0 comments on commit 2880e30

Please sign in to comment.