PHP password_hash() Function
PHP Password Hashing Algorithm
The password_hash() function is used to create a password hash.
PHP version requirement: PHP 5 >= 5.5.0, PHP 7
Syntax
string password_hash ( string $password , int $algo [, array $options ] )
password_hash() creates a password hash using a one-way hashing algorithm of sufficient strength. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can also be used with password_hash().
Currently supported algorithms:
PASSWORD_DEFAULT- Uses the bcrypt algorithm (default since PHP 5.5.0). Note that this constant may change as PHP adds newer, stronger algorithms. Therefore, the length of the result generated using this constant will change in the future. Hence, the database column storing the result can be longer than 60 characters (255 characters is preferable).PASSWORD_BCRYPT- UsesCRYPT_BLOWFISHalgorithm to create the hash. This produces a hash compatible with crypt() using "$2y$". The result will be a 60-character string, or FALSE on failureFALSE.PASSWORD_ARGON2I- Uses the Argon2 hashing algorithm to create the hash.
PASSWORD_BCRYPT Supported Options:
-
salt(string) - Manually provide the salt value for the hashed password. This avoids automatically generating a salt.
After omitting this value, password_hash() will automatically generate a random salt for each password hash. This behavior is intentional.
Note: The salt option has been deprecated since PHP 7.0.0. It is now best to simply use the default generated salt.
-
cost (integer) - Represents the cost used by the algorithm. The crypt() page has examples of cost values.
When omitted, the default value is 10. This cost is a good baseline, but you may increase it based on your hardware.
PASSWORD_ARGON2I Supported Options:
-
memory_cost (integer) - The maximum memory (in bytes) used when computing the Argon2 hash. Default value:
PASSWORD_ARGON2_DEFAULT_MEMORY_COST. -
time_cost (integer) - The maximum time used when computing the Argon2 hash. Default value:
PASSWORD_ARGON2_DEFAULT_TIME_COST. -
threads (integer) - The maximum number of threads used when computing the Argon2 hash. Default value:
PASSWORD_ARGON2_DEFAULT_THREADS.
Parameter Description:
-
password: a hash created by password_hash().
-
algo: a password algorithm constant used to indicate the algorithm when hashing the password.
-
options: an associative array containing options. Currently two options are supported: salt, added when hashing the password (a disturbance string), and cost, used to indicate the recursion depth of the algorithm. Examples of these two values can be found on the crypt() page.
If omitted, a random salt and the default cost will be used.
Return Value
Returns the hashed password, or FALSE on failure.
Example
Example 1
The output is:
$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a
Example 2
12 , ] ; echo password_hash ( " rasmuslerdorf " , PASSWORD_BCRYPT , $options ) ; ?>The output is:
$2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K
Example 3
Example of Manually Setting a Salt Value
11 , ' salt ' => mcrypt_create_iv ( 22 , MCRYPT_DEV_URANDOM ) , ] ; echo password_hash ( " rasmuslerdorf " , PASSWORD_BCRYPT , $options ) ; ?>The output is:
$2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
Example 4
Example of Finding the Best cost for password_hash()
$cost ] ) ; $end = microtime ( true ) ; } while ( ( $end - $start ) < $timeTarget ) ; echo " Appropriate Cost Found: " . $cost ; ?>The output is:
Appropriate Cost Found: 10
Example 5
Example Using Argon2:
The output is:
Argon2 hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0