Skip to content

Commit b5056b3

Browse files
committed
Merge pull request #1936 from elnur/bcrypt-password-encoder
[Waiting Code Merge] Added a section on the BCrypt password encoder
2 parents f239002 + 31c2793 commit b5056b3

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

book/security.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,74 @@ from the hashed password).
13381338
.. versionadded:: 2.2
13391339
As of Symfony 2.2 you can also use the PBKDF2 password encoder.
13401340

1341+
Using the BCrypt Password Encoder
1342+
.................................
1343+
1344+
.. versionadded:: 2.2
1345+
The BCrypt password encoder was added in Symfony 2.2.
1346+
1347+
.. configuration-block::
1348+
1349+
.. code-block:: yaml
1350+
1351+
# app/config/security.yml
1352+
security:
1353+
# ...
1354+
encoders:
1355+
Symfony\Component\Security\Core\User\User:
1356+
algorithm: bcrypt
1357+
cost: 15
1358+
1359+
.. code-block:: xml
1360+
1361+
<!-- app/config/security.xml -->
1362+
<config>
1363+
<!-- ... -->
1364+
<encoder
1365+
class="Symfony\Component\Security\Core\User\User"
1366+
algorithm="bcrypt"
1367+
cost="15"
1368+
/>
1369+
</config>
1370+
1371+
.. code-block:: php
1372+
1373+
// app/config/security.php
1374+
$container->loadFromExtension('security', array(
1375+
// ...
1376+
'encoders' => array(
1377+
'Symfony\Component\Security\Core\User\User' => array(
1378+
'algorithm' => 'bcrypt',
1379+
'cost' => 15,
1380+
),
1381+
),
1382+
));
1383+
1384+
``cost`` can be in the range of ``4-31`` and determines how long a password
1385+
will be encoded. Each increment of ``cost`` *doubles* the time it takes to
1386+
encode a password.
1387+
1388+
If you don't provide the ``cost`` option, the default cost of ``13`` is used.
1389+
1390+
.. note::
1391+
1392+
You can change the cost at any time — even if you already have some
1393+
passwords encoded using a different cost. New passwords will be encoded
1394+
using the new cost, while the already encoded ones will be validated
1395+
using a cost that was used back when they were encoded.
1396+
1397+
A salt for each new password is generated automatically and need not be
1398+
persisted. Since an encoded password contains the salt used to encode it,
1399+
persisting the encoded password alone is enough.
1400+
1401+
.. note::
1402+
1403+
All the encoded passwords are ``60`` characters long, so make sure to
1404+
allocate enough space for them to be persisted.
1405+
1406+
Determining the Hashed Password
1407+
...............................
1408+
13411409
If you have some sort of registration form for users, you'll need to be able
13421410
to determine the hashed password so that you can set it on your user. No
13431411
matter what algorithm you configure for your user object, the hashed password

0 commit comments

Comments
 (0)