Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP password_hash() Function

PHP password_hash() Function

PHP Password Hashing AlgorithmsPHP 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_BCRYPT Supported Options:

PASSWORD_ARGON2I Supported Options:

Parameter Description:

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

PHP Password Hashing AlgorithmsPHP Password Hashing Algorithm