Skip to content

[Waiting Code Merge] Added a section on the BCrypt password encoder #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions book/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,74 @@ from the hashed password).
.. versionadded:: 2.2
As of Symfony 2.2 you can also use the PBKDF2 password encoder.

Using the BCrypt Password Encoder
.................................

.. versionadded:: 2.2
The BCrypt password encoder was added in Symfony 2.2.

.. configuration-block::

.. code-block:: yaml

# app/config/security.yml
security:
# ...
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 15

.. code-block:: xml

<!-- app/config/security.xml -->
<config>
<!-- ... -->
<encoder
class="Symfony\Component\Security\Core\User\User"
algorithm="bcrypt"
cost="15"
/>
</config>

.. code-block:: php

// app/config/security.php
$container->loadFromExtension('security', array(
// ...
'encoders' => array(
'Symfony\Component\Security\Core\User\User' => array(
'algorithm' => 'bcrypt',
'cost' => 15,
),
),
));

``cost`` can be in the range of ``4-31`` and determines how long a password
will be encoded. Each increment of ``cost`` *doubles* the time it takes to
encode a password.

If you don't provide the ``cost`` option, the default cost of ``13`` is used.

.. note::

You can change the cost at any time — even if you already have some
passwords encoded using a different cost. New passwords will be encoded
using the new cost, while the already encoded ones will be validated
using a cost that was used back when they were encoded.

A salt for each new password is generated automatically and need not be
persisted. Since an encoded password contains the salt used to encode it,
persisting the encoded password alone is enough.

.. note::

All the encoded passwords are ``60`` characters long, so make sure to
allocate enough space for them to be persisted.

Determining the Hashed Password
...............................

If you have some sort of registration form for users, you'll need to be able
to determine the hashed password so that you can set it on your user. No
matter what algorithm you configure for your user object, the hashed password
Expand Down