BlowfishPasswordHash implements PasswordHashInterface
Class that implements Blowfish salted hashing based on PHP's crypt() function.
Warning: Blowfish salted hashing with PHP's crypt() is not available on every system.
Table of Contents
Interfaces
- PasswordHashInterface
- Interface with public methods needed to be implemented in a salting hashing class.
Constants
- PREFIX = '$2a$'
- Prefix for the password hash.
Properties
- $options : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructor sets options if given
- checkPassword() : bool
- Method checks if a given plaintext password is correct by comparing it with a given salted hashed password.
- getHashedPassword() : string
- Method creates a salted hash for a given plaintext password
- isAvailable() : bool
- Returns whether all prerequisites for the hashing methods are matched
- isHashUpdateNeeded() : bool
- Checks whether a user's hashed password needs to be replaced with a new hash.
- isValidSaltedPW() : bool
- Method determines if a given string is a valid salted hashed password.
- applySettingsToSalt() : string
- Method applies settings (prefix, hash count) to a salt.
- base64Encode() : string
- Encodes bytes into printable base 64 using the *nix standard from crypt().
- getCountLog2() : int
- Parses the log2 iteration count from a stored hash or setting string.
- getGeneratedSalt() : string
- Generates a random base 64-encoded salt prefixed and suffixed with settings for the hash.
- getItoa64() : string
- Returns a string for mapping an int to the corresponding base 64 character.
- getLengthBase64FromBytes() : int
- Method determines required length of base64 characters for a given length of a byte string.
- isValidSalt() : bool
- Method determines if a given string is a valid salt.
Constants
PREFIX
Prefix for the password hash.
protected
mixed
PREFIX
= '$2a$'
Properties
$options
protected
array<string|int, mixed>
$options
= ['hash_count' => 7]
The default log2 number of iterations for password stretching.
Methods
__construct()
Constructor sets options if given
public
__construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
Tags
checkPassword()
Method checks if a given plaintext password is correct by comparing it with a given salted hashed password.
public
checkPassword(string $plainPW, string $saltedHashPW) : bool
Parameters
- $plainPW : string
-
plain-text password to compare with salted hash
- $saltedHashPW : string
-
salted hash to compare plain-text password with
Return values
bool —TRUE, if plain-text password matches the salted hash, otherwise FALSE
getHashedPassword()
Method creates a salted hash for a given plaintext password
public
getHashedPassword(string $password) : string
Parameters
- $password : string
-
plaintext password to create a salted hash from
Return values
string —Salted hashed password
isAvailable()
Returns whether all prerequisites for the hashing methods are matched
public
isAvailable() : bool
Return values
bool —Method available
isHashUpdateNeeded()
Checks whether a user's hashed password needs to be replaced with a new hash.
public
isHashUpdateNeeded(string $saltedPW) : bool
This is typically called during the login process when the plain text password is available. A new hash is needed when the desired iteration count has changed through a change in the variable $hashCount or HASH_COUNT.
Parameters
- $saltedPW : string
-
Salted hash to check if it needs an update
Return values
bool —TRUE if salted hash needs an update, otherwise FALSE
isValidSaltedPW()
Method determines if a given string is a valid salted hashed password.
public
isValidSaltedPW(string $saltedPW) : bool
Parameters
- $saltedPW : string
-
String to check
Return values
bool —TRUE if it's valid salted hashed password, otherwise FALSE
applySettingsToSalt()
Method applies settings (prefix, hash count) to a salt.
protected
applySettingsToSalt(string $salt) : string
Parameters
- $salt : string
-
A salt to apply setting to
Return values
string —Salt with setting
base64Encode()
Encodes bytes into printable base 64 using the *nix standard from crypt().
protected
base64Encode(string $input, int $count) : string
Parameters
- $input : string
-
The string containing bytes to encode.
- $count : int
-
The number of characters (bytes) to encode.
Return values
string —Encoded string
getCountLog2()
Parses the log2 iteration count from a stored hash or setting string.
protected
getCountLog2(string $setting) : int
Parameters
- $setting : string
-
Complete hash or a hash's setting string or to get log2 iteration count from
Return values
int —Used hashcount for given hash string
getGeneratedSalt()
Generates a random base 64-encoded salt prefixed and suffixed with settings for the hash.
protected
getGeneratedSalt() : string
Proper use of salts may defeat a number of attacks, including:
- The ability to try candidate passwords against multiple hashes at once.
- The ability to use pre-hashed lists of candidate passwords.
- The ability to determine whether two users have the same (or different) password without actually having to guess one of the passwords.
Return values
string —A character string containing settings and a random salt
getItoa64()
Returns a string for mapping an int to the corresponding base 64 character.
protected
getItoa64() : string
Return values
string —String for mapping an int to the corresponding base 64 character
getLengthBase64FromBytes()
Method determines required length of base64 characters for a given length of a byte string.
protected
getLengthBase64FromBytes(int $byteLength) : int
Parameters
- $byteLength : int
-
Length of bytes to calculate in base64 chars
Return values
int —Required length of base64 characters
isValidSalt()
Method determines if a given string is a valid salt.
protected
isValidSalt(string $salt) : bool
Parameters
- $salt : string
-
String to check
Return values
bool —TRUE if it's valid salt, otherwise FALSE