class UserAuthenticationService (View source)

Class UserAuthenticationService

Provides various methods for managing password hashing and verification.

Constants

private BCRYPT_MIN_COST

private BCRYPT_MAX_COST

private DUMMY_HASH_BODY

Methods

__construct(string $passwordEncryption = 'bcrypt', int $passwordEncryptionCost = 12)

No description

string
dummyVerificationHash()

A well-formed bcrypt hash that no submitted password is expected to match, at the configured cost.

string
generateSalt(int $len = 5)

Generate a random salt string.

string
hashPassword(string $password)

Hash a password using the specified method.

bool
verifyPassword(string $password, string $hash)

Verify if a password matches the hashed password.

bool
requiresRehash(string $hash)

Check if the hashed password requires rehashing.

string
identifyHashAlgorithm(string $hash)

Identify the hash algorithm used for the given hash.

string
generateCombinedSalt(string $pass)

Generate a combined salt for the given password.

string
combineSalts(string $salt, string $pass)

Combine a salt with a password.

string
extractUserSalt(string $password)

Extract the user salt from the given password.

Details

__construct(string $passwordEncryption = 'bcrypt', int $passwordEncryptionCost = 12)

No description

Parameters

string $passwordEncryption
int $passwordEncryptionCost

string dummyVerificationHash()

A well-formed bcrypt hash that no submitted password is expected to match, at the configured cost.

Callers use it to spend the same time on a login for a user that does not exist as on one that does. Only the cost digits are substituted - the salt and digest stay valid, so password_verify() runs the full key derivation before returning false, and no hash has to be computed per request.

Always bcrypt: an argon2i/argon2id install still gets most of the delay, which is the point, without needing a constant per parameter set.

Return Value

string

string generateSalt(int $len = 5)

Generate a random salt string.

Draws from random_int(): the installer builds the session encryption key with this, and mt_rand() would cap that key at its 32-bit seed.

Parameters

int $len

The length of the salt string (default is 5).

Return Value

string

The generated salt string.

string hashPassword(string $password)

Hash a password using the specified method.

Parameters

string $password

The password to be hashed.

Return Value

string

The hashed password.

Exceptions

InvalidArgumentException

bool verifyPassword(string $password, string $hash)

Verify if a password matches the hashed password.

Parameters

string $password

The password to be verified.

string $hash

The hashed password.

Return Value

bool

True if the password matches, false otherwise.

Exceptions

InvalidArgumentException

bool requiresRehash(string $hash)

Check if the hashed password requires rehashing.

Parameters

string $hash

The hashed password.

Return Value

bool

True if the password needs rehashing, false otherwise.

Exceptions

InvalidArgumentException

string identifyHashAlgorithm(string $hash)

Identify the hash algorithm used for the given hash.

Parameters

string $hash

The hashed password.

Return Value

string

The hash algorithm name.

Exceptions

InvalidArgumentException

string generateCombinedSalt(string $pass)

Generate a combined salt for the given password.

Parameters

string $pass

The password.

Return Value

string

The combined salt.

string combineSalts(string $salt, string $pass)

Combine a salt with a password.

Parameters

string $salt

The salt.

string $pass

The password.

Return Value

string

The combined salt and password.

string extractUserSalt(string $password)

Extract the user salt from the given password.

Parameters

string $password

The password.

Return Value

string

The extracted salt.