Skip to content

[PasswordHasher] Mention standalone use of PasswordHasherFactory #18133

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
Mar 30, 2023
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
28 changes: 27 additions & 1 deletion security/passwords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Further in this article, you can find a
.. configuration-block::

.. code-block:: yaml

# config/packages/test/security.yaml
security:
# ...
Expand Down Expand Up @@ -697,6 +697,32 @@ you must register a service for it in order to use it as a named hasher:
This creates a hasher named ``app_hasher`` from a service with the ID
``App\Security\Hasher\MyCustomPasswordHasher``.

Hashing a Stand-Alone String
----------------------------

The password hasher can be used to hash strings independently
of users. By using the
:class:`Symfony\\Component\\PasswordHasher\\Hasher\\PasswordHasherFactory`,
you can declare multiple hashers, retrieve any of them with
its name and create hashes. You can then verify that a string matches the given
hash::

use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;

// configure different hashers via the factory
$factory = new PasswordHasherFactory([
'common' => ['algorithm' => 'bcrypt'],
'sodium' => ['algorithm' => 'sodium'],
]);

// retrieve the hasher using bcrypt
$hasher = $factory->getPasswordHasher('common');
$hash = $hasher->hash('plain');

// verify that a given string matches the hash calculated above
$hasher->verify($hash, 'invalid'); // false
$hasher->verify($hash, 'plain'); // true

.. _passwordhasher-supported-algorithms:

Supported Algorithms
Expand Down