Skip to content

Commit b29832e

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [PasswordHasher] Mention standalone use of PasswordHasherFactory
2 parents 1efb062 + e380744 commit b29832e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

security/passwords.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,32 @@ you must register a service for it in order to use it as a named hasher:
695695
This creates a hasher named ``app_hasher`` from a service with the ID
696696
``App\Security\Hasher\MyCustomPasswordHasher``.
697697

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

700726
Supported Algorithms

0 commit comments

Comments
 (0)