Skip to content

DefaultPasswordHasher

Thin wrapper over PHP’s password_hash() family: argon2id by default, falling back to bcrypt only when the running PHP build lacks argon2 support (no libargon2 at compile time).

final class DefaultPasswordHasher implements PasswordHasherInterface

ImplementsPasswordHasherInterface
Since1.0.0
SourceHasher/DefaultPasswordHasher.php

public function __construct(?string $algorithm = null, array<string, mixed> $options = []): mixed

Passed through to password_hash()/password_needs_rehash().

ParameterTypeDescription
$algorithm?``stringOne of the PASSWORD_* constants; defaults to argon2id (or bcrypt if unavailable).
$optionsarray``<``string``, ``mixed``>Passed through to password_hash()/password_needs_rehash().

Returns mixed

ThrowsWhen
InvalidArgumentExceptionIf $algorithm is neither PASSWORD_BCRYPT nor (when available) PASSWORD_ARGON2ID.
MethodDescription
hash(string $plaintext): string
needsRehash(string $hash): bool
verify(string $plaintext, string $hash): bool

public function hash(string $plaintext): string

The plaintext password to hash.

ParameterTypeDescription
$plaintextstringThe plaintext password to hash.

Returns string — The resulting hash, suitable for storage.

public function needsRehash(string $hash): bool

A previously-stored hash (see hash()).

ParameterTypeDescription
$hashstringA previously-stored hash (see hash()).

Returns bool — True if $hash was produced with weaker-than-current-default parameters, otherwise false.

public function verify(string $plaintext, string $hash): bool

A previously-stored hash (see hash()).

ParameterTypeDescription
$plaintextstringThe plaintext password to check.
$hashstringA previously-stored hash (see hash()).

Returns bool — True if $plaintext matches $hash, otherwise false.