8000 [Security] Added Pbkdf2PasswordEncoder · s7ntech/symfony@4534960 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4534960

Browse files
committed
[Security] Added Pbkdf2PasswordEncoder
[Security] changed default iterations of Pbkdf2PasswordEncoder to 1000 instead of 5000 [Security] Improved description of PBKDF2 encoder [SecurityBundle] added PBKDF2 PasswordEncoder updated CHANGELOG.md [Security] Use the build-in hash_pbkdf2() when available [SecurityBundle] added information about hash_algorithm for configuration [Security] always check algorithm and fixed CS
1 parent f187bee commit 4534960

File tree

11 files changed

+189
-0
lines changed

11 files changed

+189
-0
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* Added PBKDF2 Password encoder
8+
49
2.1.0
510
-----
611

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ private function addEncodersSection(ArrayNodeDefinition $rootNode)
378378
->beforeNormalization()->ifString()->then(function($v) { return array('algorithm' => $v); })->end()
379379
->children()
380380
->scalarNode('algorithm')->cannotBeEmpty()->end()
381+
->scalarNode('hash_algorithm')->info('Name of hashing algorithm for PBKDF2 (i.e. sha256, sha512, etc..) See hash_algos() for a list of supported algorithms.')->defaultValue('sha512')->end()
382+
->scalarNode('key_length')->defaultValue(40)->end()
381383
->booleanNode('ignore_case')->defaultFalse()->end()
382384
->booleanNode('encode_as_base64')->defaultTrue()->end()
383385
->scalarNode('iterations')->defaultValue(5000)->end()

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,21 @@ private function createEncoder($config, ContainerBuilder $container)
449449
);
450450
}
451451

452+
// pbkdf2 encoder
453+
if ('pbkdf2' === $config['algorithm']) {
454+
$arguments = array(
455+
$config['hash_algorithm'],
456+
$config['encode_as_base64'],
457+
$config['iterations'],
458+
$config['key_length'],
459+
);
460+
461+
return array(
462+
'class' => new Parameter('security.encoder.pbkdf2.class'),
463+
'arguments' => $arguments,
464+
);
465+
}
466+
452467
// message digest encoder
453468
$arguments = array(
454469
$config['algorithm'],

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<parameter key="security.encoder_factory.generic.class">Symfony\Component\Security\Core\Encoder\EncoderFactory</parameter>
1313
<parameter key="security.encoder.digest.class">Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder</parameter>
1414
<parameter key="security.encoder.plain.class">Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder</parameter>
15+
<parameter key="security.encoder.pbkdf2.class">Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder</parameter>
1516

1617
<parameter key="security.user.provider.in_memory.class">Symfony\Component\Security\Core\User\InMemoryUserProvider</parameter>
1718
<parameter key="security.user.provider.in_memory.user.class">Symfony\Component\Security\Core\User\User</parameter>

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
'JMS\FooBundle\Entity\User4' => array(
1616
'id' => 'security.encoder.foo',
1717
),
18+
'JMS\FooBundle\Entity\User5' => array(
19+
'algorithm' => 'pbkdf2',
20+
'hash_algorithm' => 'sha1',
21+
'encode_as_base64' => false,
22+
'iterations' => 5,
23+
'key_length' => 30,
24+
),
1825
),
1926
'providers' => array(
2027
'default' => array(

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
<encoder class="JMS\FooBundle\Entity\User4" id="security.encoder.foo" />
1818

19+
<encoder class="JMS\FooBundle\Entity\User5" algorithm="pbkdf2" hash-algorithm="sha1" encode-as-base64="false" iterations="5" key-length="30" />
20+
1921
<provider name="default">
2022
<memory>
2123
<user name="foo" password="foo" roles="ROLE_USER" />

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ security:
1010
algorithm: md5
1111
JMS\FooBundle\Entity\User4:
1212
id: security.encoder.foo
13+
JMS\FooBundle\Entity\User5:
14+
algorithm: pbkdf2
15+
hash_algorithm: sha1
16+
encode_as_base64: false
17+
iterations: 5
18+
key_length: 30
1319

1420
providers:
1521
default:

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public function testEncoders()
145145
'arguments' => array('md5', true, 5000),
146146
),
147147
'JMS\FooBundle\Entity\User4' => new Reference('security.encoder.foo'),
148+
'JMS\FooBundle\Entity\User5' => array(
149+
'class' => new Parameter('security.encoder.pbkdf2.class'),
150+
'arguments' => array('sha1', false, 5, 30),
151+
),
148152
)), $container->getDefinition('security.encoder_factory.generic')->getArguments());
149153
}
150154

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* Added PBKDF2 Password encoder
8+
49
2.1.0
510
-----
611

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Encoder;
13+
14+
/**
15+
* Pbkdf2PasswordEncoder uses the PBKDF2 (Password-Based Key Derivation Function 2).
16+
*
17+
* Providing a high level of Cryptographic security,
18+
* PBKDF2 is recommended by the National Institute of Standards and Technology (NIST).
19+
*
20+
* But also warrants a warning, using PBKDF2 (with a high number of iterations) slows down the process.
21+
* PBKDF2 should be used with caution and care.
22+
*
23+
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
24+
* @author Andrew Johnson
25+
* @author Fabien Potencier <fabien@symfony.com>
26+
*/
27+
class Pbkdf2PasswordEncoder extends BasePasswordEncoder
28+
{
29+
private $algorithm;
30+
private $encodeHashAsBase64;
31+
private $iterations;
32+
private $length;
33+
34+
/**
35+
* Constructor.
36+
*
37+
* @param string $algorithm The digest algorithm to use
38+
* @param Boolean $encodeHashAsBase64 Whether to base64 encode the password hash
39+
* @param integer $iterations The number of iterations to use to stretch the password hash
40+
* @param integer $length Length of derived key to create
41+
*/
42+
public function __construct($algorithm = 'sha512', $encodeHashAsBase64 = true, $iterations = 1000, $length = 40)
43+
{
44+
$this->algorithm = $algorithm;
45+
$this->encodeHashAsBase64 = $encodeHashAsBase64;
46+
$this->iterations = $iterations;
47+
$this->length = $length;
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*
53+
* @throws \LogicException when the algorithm is not supported
54+
*/
55+
public function encodePassword($raw, $salt)
56+
{
57+
if (!in_array($this->algorithm, hash_algos(), true)) {
58+
throw new \LogicException(sprintf('The algorithm "%s" is not supported.', $this->algorithm));
59+
}
60+
61+
if (function_exists('hash_pbkdf2')) {
62+
$digest = hash_pbkdf2($this->algorithm, $raw, $salt, $this->iterations, $this->length, true);
63+
} else {
64+
$digest = $this->hashPbkdf2($this->algorithm, $raw, $salt, $this->iterations, $this->length);
65+
}
66+
67+
return $this->encodeHashAsBase64 ? base64_encode($digest) : bin2hex($digest);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function isPasswordValid($encoded, $raw, $salt)
74+
{
75+
return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
76+
}
77+
78+
private function hashPbkdf2($algorithm, $password, $salt, $iterations, $length = 0)
79+
{
80+
// Number of blocks needed to create the derived key
81+
$blocks = ceil($length / strlen(hash($algorithm, null, true)));
82+
$digest = '';
83+
84+
for ($i = 1; $i <= $blocks; $i++) {
85+< 79A4 /span>
$ib = $block = hash_hmac($algorithm, $salt . pack('N', $i), $password, true);
86+
87+
// Iterations
88+
for ($j = 1; $j < $iterations; $j++) {
89+
$ib ^= ($block = hash_hmac($algorithm, $block, $password, true));
90+
}
91+
92+
$digest .= $ib;
93+
}
94+
95+
return substr($digest, 0, $this->length);
96+
}
97+
}

0 commit comments

Comments
 (0)
0