Skip to content

Commit e803df6

Browse files
[PasswordHasher] Mention standalone use of PasswordHasherFactory
1 parent 7322465 commit e803df6

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

security/passwords.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Further in this article, you can find a
134134
.. configuration-block::
135135

136136
.. code-block:: yaml
137-
137+
138138
# config/packages/test/security.yaml
139139
security:
140140
# ...
@@ -697,6 +697,32 @@ you must register a service for it in order to use it as a named hasher:
697697
This creates a hasher named ``app_hasher`` from a service with the ID
698698
``App\Security\Hasher\MyCustomPasswordHasher``.
699699

700+
Hashing a Stand-Alone String
701+
----------------------------
702+
703+
The password hasher can be used to hash strings independently
704+
of users. By using the
705+
:class:`Symfony\\Component\\PasswordHasher\\Hasher\\PasswordHasherFactory`,
706+
you can retrieve declare multiple hashers, retrieve any of them with
707+
its name and create hashes. You can then verify that a string matches the given
708+
hash::
709+
710+
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
711+
712+
// configure different password hashers via the factory
713+
$factory = new PasswordHasherFactory([
714+
'common' => ['algorithm' => 'bcrypt'],
715+
'sodium' => ['algorithm' => 'sodium'],
716+
]);
717+
718+
// retrieve the hasher using bcrypt
719+
$passwordHasher = $factory->getPasswordHasher('common');
720+
$hash = $passwordHasher->hash('plain');
721+
722+
// verify that a given plain password matches the hash calculated above
723+
$passwordHasher->verify($hash, 'invalid'); // false
724+
$passwordHasher->verify($hash, 'plain'); // true
725+
700726
.. _passwordhasher-supported-algorithms:
701727

702728
Supported Algorithms

0 commit comments

Comments
 (0)