Skip to content

Commit b12a4b5

Browse files
committed
[Security] Renamed encoders to password_hashers
1 parent 1cb4e7f commit b12a4b5

File tree

4 files changed

+130
-112
lines changed

4 files changed

+130
-112
lines changed

reference/configuration/security.rst

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Some of these options define tens of sub-options and they are explained in
4040
separate articles:
4141

4242
* `access_control`_
43-
* `encoders`_
43+
* `hashers`_
4444
* `firewalls`_
4545
* `providers`_
4646
* `role_hierarchy`_
@@ -120,15 +120,16 @@ and to allow anonymous users to the login form page.
120120

121121
This option is explained in detail in :doc:`/security/access_control`.
122122

123-
encoders
124-
--------
123+
.. _encoders:
125124

126-
This option defines the algorithm used to *encode* the password of the users.
127-
Although Symfony calls it *"password encoding"* for historical reasons, this is
128-
in fact, *"password hashing"*.
125+
hashers
126+
-------
127+
128+
This option defines the algorithm used to *hash* the password of the users
129+
(which in previous Symfony versions was wrongly called *"password encoding"*).
129130

130131
If your app defines more than one user class, each of them can define its own
131-
encoding algorithm. Also, each algorithm defines different config options:
132+
hashing algorithm. Also, each algorithm defines different config options:
132133

133134
.. configuration-block::
134135

@@ -138,25 +139,25 @@ encoding algorithm. Also, each algorithm defines different config options:
138139
security:
139140
# ...
140141
141-
encoders:
142-
# auto encoder with default options
142+
password_hashers:
143+
# auto hasher with default options
143144
App\Entity\User: 'auto'
144145
145-
# auto encoder with custom options
146+
# auto hasher with custom options
146147
App\Entity\User:
147148
algorithm: 'auto'
148149
cost: 15
149150
150-
# Sodium encoder with default options
151+
# Sodium hasher with default options
151152
App\Entity\User: 'sodium'
152153
153-
# Sodium encoder with custom options
154+
# Sodium hasher with custom options
154155
App\Entity\User:
155156
algorithm: 'sodium'
156157
memory_cost: 16384 # Amount in KiB. (16384 = 16 MiB)
157158
time_cost: 2 # Number of iterations
158159
159-
# MessageDigestPasswordEncoder encoder using SHA512 hashing with default options
160+
# MessageDigestPasswordHasher hasher using SHA512 hashing with default options
160161
App\Entity\User: 'sha512'
161162
162163
.. code-block:: xml
@@ -173,37 +174,37 @@ encoding algorithm. Also, each algorithm defines different config options:
173174
174175
<config>
175176
<!-- ... -->
176-
<!-- auto encoder with default options -->
177-
<encoder
177+
<!-- auto hasher with default options -->
178+
<security:password-hasher
178179
class="App\Entity\User"
179180
algorithm="auto"
180181
/>
181182
182-
<!-- auto encoder with custom options -->
183-
<encoder
183+
<!-- auto hasher with custom options -->
184+
<security:password-hasher
184185
class="App\Entity\User"
185186
algorithm="auto"
186187
cost="15"
187188
/>
188189
189-
<!-- Sodium encoder with default options -->
190-
<encoder
190+
<!-- Sodium hasher with default options -->
191+
<security:password-hasher
191192
class="App\Entity\User"
192193
algorithm="sodium"
193194
/>
194195
195-
<!-- Sodium encoder with custom options -->
196+
<!-- Sodium hasher with custom options -->
196197
<!-- memory_cost: amount in KiB. (16384 = 16 MiB)
197198
time_cost: number of iterations -->
198-
<encoder
199+
<security:password-hasher
199200
class="App\Entity\User"
200201
algorithm="sodium"
201202
memory_cost="16384"
202203
time_cost="2"
203204
/>
204205
205-
<!-- MessageDigestPasswordEncoder encoder using SHA512 hashing with default options -->
206-
<encoder
206+
<!-- MessageDigestPasswordHasher hasher using SHA512 hashing with default options -->
207+
<security:password-hasher
207208
class="App\Entity\User"
208209
algorithm="sha512"
209210
/>
@@ -217,55 +218,61 @@ encoding algorithm. Also, each algorithm defines different config options:
217218
218219
$container->loadFromExtension('security', [
219220
// ...
220-
'encoders' => [
221-
// auto encoder with default options
221+
'password_hashers' => [
222+
// auto hasher with default options
222223
User::class => [
223224
'algorithm' => 'auto',
224225
],
225226
226-
// auto encoder with custom options
227+
// auto hasher with custom options
227228
User::class => [
228229
'algorithm' => 'auto',
229230
'cost' => 15,
230231
],
231232
232-
// Sodium encoder with default options
233+
// Sodium hasher with default options
233234
User::class => [
234235
'algorithm' => 'sodium',
235236
],
236237
237-
// Sodium encoder with custom options
238+
// Sodium hasher with custom options
238239
User::class => [
239240
'algorithm' => 'sodium',
240241
'memory_cost' => 16384, // Amount in KiB. (16384 = 16 MiB)
241242
'time_cost' => 2, // Number of iterations
242243
],
243244
244-
// MessageDigestPasswordEncoder encoder using SHA512 hashing with default options
245+
// MessageDigestPasswordHasher hasher using SHA512 hashing with default options
245246
User::class => [
246247
'algorithm' => 'sha512',
247248
],
248249
],
249250
]);
250251
252+
.. versionadded:: 5.3
253+
254+
The ``password_hashers`` option was introduced in Symfony 5.3. In previous
255+
versions it was called ``encoders``.
256+
251257
.. tip::
252258

253-
You can also create your own password encoders as services and you can even
254-
select a different password encoder for each user instance. Read
259+
You can also create your own password hashers as services and you can even
260+
select a different password hasher for each user instance. Read
255261
:doc:`this article </security/named_encoders>` for more details.
256262

257263
.. tip::
258264

259-
Encoding passwords is resource intensive and takes time in order to generate
265+
Hashing passwords is resource intensive and takes time in order to generate
260266
secure password hashes. In tests however, secure hashes are not important, so
261-
you can change the encoders configuration in ``test`` environment to run tests faster:
267+
you can change the password hasher configuration in ``test`` environment to
268+
run tests faster:
262269

263270
.. configuration-block::
264271

265272
.. code-block:: yaml
266273
267274
# config/packages/test/security.yaml
268-
encoders:
275+
password_hashers:
269276
# Use your user class name here
270277
App\Entity\User:
271278
algorithm: auto # This should be the same value as in config/packages/security.yaml
@@ -289,7 +296,7 @@ encoding algorithm. Also, each algorithm defines different config options:
289296
<!-- cost: Lowest possible value for bcrypt -->
290297
<!-- time_cost: Lowest possible value for argon -->
291298
<!-- memory_cost: Lowest possible value for argon -->
292-
<encoder
299+
<security:password-hasher
293300
class="App\Entity\User"
294301
algorithm="auto"
295302
cost="4"
@@ -305,7 +312,7 @@ encoding algorithm. Also, each algorithm defines different config options:
305312
use App\Entity\User;
306313
307314
$container->loadFromExtension('security', [
308-
'encoders' => [
315+
'password_hashers' => [
309316
// Use your user class name here
310317
User::class => [
311318
'algorithm' => 'auto', // This should be the same value as in config/packages/security.yaml
@@ -318,44 +325,46 @@ encoding algorithm. Also, each algorithm defines different config options:
318325
319326
.. _reference-security-sodium:
320327
.. _using-the-argon2i-password-encoder:
328+
.. _using-the-sodium-password-encoder:
321329

322-
Using the Sodium Password Encoder
323-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
330+
Using the Sodium Password Hasher
331+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
324332

325-
It uses the `Argon2 key derivation function`_ and it's the encoder recommended
333+
It uses the `Argon2 key derivation function`_ and it's the hasher recommended
326334
by Symfony. Argon2 support was introduced in PHP 7.2, but if you use an earlier
327335
PHP version, you can install the `libsodium`_ PHP extension.
328336

329-
The encoded passwords are ``96`` characters long, but due to the hashing
337+
The hashed passwords are ``96`` characters long, but due to the hashing
330338
requirements saved in the resulting hash this may change in the future, so make
331339
sure to allocate enough space for them to be persisted. Also, passwords include
332340
the `cryptographic salt`_ inside them (it's generated automatically for each new
333341
password) so you don't have to deal with it.
334342

335343
.. _reference-security-encoder-auto:
344+
.. _using-the-auto-password-encoder:
336345

337-
Using the "auto" Password Encoder
338-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
346+
Using the "auto" Password Hasher
347+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339348

340-
It selects automatically the best possible encoder. Currently, it tries to use
349+
It selects automatically the best possible hasher. Currently, it tries to use
341350
Sodium by default and falls back to the `bcrypt password hashing function`_ if
342351
not possible. In the future, when PHP adds new hashing techniques, it may use
343352
different password hashers.
344353

345-
It produces encoded passwords with ``60`` characters long, so make sure to
354+
It produces hashed passwords with ``60`` characters long, so make sure to
346355
allocate enough space for them to be persisted. Also, passwords include the
347356
`cryptographic salt`_ inside them (it's generated automatically for each new
348357
password) so you don't have to deal with it.
349358

350359
Its only configuration option is ``cost``, which is an integer in the range of
351360
``4-31`` (by default, ``13``). Each single increment of the cost **doubles the
352-
time** it takes to encode a password. It's designed this way so the password
361+
time** it takes to hash a password. It's designed this way so the password
353362
strength can be adapted to the future improvements in computation power.
354363

355364
You can change the cost at any time — even if you already have some passwords
356-
encoded using a different cost. New passwords will be encoded using the new
357-
cost, while the already encoded ones will be validated using a cost that was
358-
used back when they were encoded.
365+
hashed using a different cost. New passwords will be hashed using the new
366+
cost, while the already hashed ones will be validated using a cost that was
367+
used back when they were hashed.
359368

360369
.. tip::
361370

@@ -364,13 +373,14 @@ used back when they were encoded.
364373
environment configuration.
365374

366375
.. _reference-security-pbkdf2:
376+
.. _using-the-pbkdf2-encoder:
367377

368-
Using the PBKDF2 Encoder
369-
~~~~~~~~~~~~~~~~~~~~~~~~
378+
Using the PBKDF2 Hasher
379+
~~~~~~~~~~~~~~~~~~~~~~~
370380

371-
Using the `PBKDF2`_ encoder is no longer recommended since PHP added support for
381+
Using the `PBKDF2`_ hasher is no longer recommended since PHP added support for
372382
Sodium and BCrypt. Legacy application still using it are encouraged to upgrade
373-
to those newer encoding algorithms.
383+
to those newer hashing algorithms.
374384

375385
firewalls
376386
---------

security.rst

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,13 @@ here: :doc:`User Providers </security/user_provider>`.
200200

201201
.. _security-encoding-user-password:
202202
.. _encoding-the-user-s-password:
203+
.. _2c-encoding-passwords:
203204

204-
2c) Encoding Passwords
205-
----------------------
205+
2c) Hashing Passwords
206+
---------------------
206207

207208
Not all applications have "users" that need passwords. *If* your users have passwords,
208-
you can control how those passwords are encoded in ``security.yaml``. The ``make:user``
209+
you can control how those passwords are hashed in ``security.yaml``. The ``make:user``
209210
command will pre-configure this for you:
210211

211212
.. configuration-block::
@@ -216,10 +217,10 @@ command will pre-configure this for you:
216217
security:
217218
# ...
218219
219-
encoders:
220+
password_hashers:
220221
# use your user class name here
221222
App\Entity\User:
222-
# Use native password encoder, which auto-selects the best
223+
# Use native password hasher, which auto-selects the best
223224
# possible hashing algorithm (starting from Symfony 5.3 this is "bcrypt")
224225
algorithm: auto
225226
@@ -238,7 +239,7 @@ command will pre-configure this for you:
238239
<config>
239240
<!-- ... -->
240241
241-
<encoder class="App\Entity\User"
242+
<security:password-hasher class="App\Entity\User"
242243
algorithm="auto"
243244
cost="12"/>
244245
@@ -254,7 +255,7 @@ command will pre-configure this for you:
254255
$container->loadFromExtension('security', [
255256
// ...
256257
257-
'encoders' => [
258+
'password_hashers' => [
258259
User::class => [
259260
'algorithm' => 'auto',
260261
'cost' => 12,
@@ -264,8 +265,13 @@ command will pre-configure this for you:
264265
// ...
265266
]);
266267
267-
Now that Symfony knows *how* you want to encode the passwords, you can use the
268-
``UserPasswordEncoderInterface`` service to do this before saving your users to
268+
.. versionadded:: 5.3
269+
270+
The ``password_hashers`` option was introduced in Symfony 5.3. In previous
271+
versions it was called ``encoders``.
272+
273+
Now that Symfony knows *how* you want to hash the passwords, you can use the
274+
``UserPasswordHasherInterface`` service to do this before saving your users to
269275
the database.
270276

271277
.. _user-data-fixture:
@@ -280,30 +286,30 @@ create dummy database users:
280286
The class name of the fixtures to create (e.g. AppFixtures):
281287
> UserFixtures
282288
283-
Use this service to encode the passwords:
289+
Use this service to hash the passwords:
284290

285291
.. code-block:: diff
286292
287293
// src/DataFixtures/UserFixtures.php
288294
289-
+ use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
295+
+ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
290296
// ...
291297
292298
class UserFixtures extends Fixture
293299
{
294-
+ private $passwordEncoder;
300+
+ private $passwordHasher;
295301
296-
+ public function __construct(UserPasswordEncoderInterface $passwordEncoder)
302+
+ public function __construct(UserPasswordHasherInterface $passwordHasher)
297303
+ {
298-
+ $this->passwordEncoder = $passwordEncoder;
304+
+ $this->passwordHasher = $passwordHasher;
299305
+ }
300306
301307
public function load(ObjectManager $manager)
302308
{
303309
$user = new User();
304310
// ...
305311
306-
+ $user->setPassword($this->passwordEncoder->encodePassword(
312+
+ $user->setPassword($this->passwordHasher->hashPassword(
307313
+ $user,
308314
+ 'the_new_password'
309315
+ ));
@@ -312,11 +318,11 @@ Use this service to encode the passwords:
312318
}
313319
}
314320
315-
You can manually encode a password by running:
321+
You can manually hash a password by running:
316322

317323
.. code-block:: terminal
318324
319-
$ php bin/console security:encode-password
325+
$ php bin/console security:hash-password
320326
321327
.. _security-yaml-firewalls:
322328
.. _security-firewalls:

0 commit comments

Comments
 (0)