Creates a password hash (original) (raw)

password_hash

(PHP 5 >= 5.5.0, PHP 7)

password_hash — Creates a password hash

Description

password_hash ( string $password , mixed $algo [, array $options ] ) : string

The following algorithms are currently supported:

Supported options for PASSWORD_BCRYPT:

Supported options for PASSWORD_ARGON2I and PASSWORD_ARGON2ID:

Parameters

password

The user's password.

Caution

Using the PASSWORD_BCRYPT as the algorithm, will result in the password parameter being truncated to a maximum length of 72 characters.

algo

A password algorithm constant denoting the algorithm to use when hashing the password.

options

An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm.

If omitted, a random salt will be created and the default cost will be used.

Return Values

Returns the hashed password, or FALSE on failure.

The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information.

Changelog

Version Description
7.4.0 The algo parameter expects a string now, but still acceptsintegers for backward compatibility.
7.3.0 Support for Argon2id passwords using PASSWORD_ARGON2ID was added.
7.2.0 Support for Argon2i passwords using PASSWORD_ARGON2I was added.

Examples

Example #1 password_hash() example

`<?php
/**

The above example will output something similar to: 2y2y2y10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a

Example #2 password_hash() example setting cost manually

`<?php
/**

The above example will output something similar to: 2y2y2y12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K

Example #3 password_hash() example finding a good cost

`<?php
/**

echo

"Appropriate Cost Found: " . $cost;
?> `

The above example will output something similar to:

Appropriate Cost Found: 10

Example #4 password_hash() example using Argon2i

<?php echo 'Argon2i hash: ' . password_hash('rasmuslerdorf', PASSWORD_ARGON2I); ?>

The above example will output something similar to:

Argon2i hash: argon2iargon2iargon2iv=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0

Notes

Caution

It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one.

As noted above, providing the salt option in PHP 7.0 will generate a deprecation warning. Support for providing a salt manually may be removed in a future PHP release.

Note:

It is recommended that you test this function on your servers, and adjust the cost parameter so that execution of the function takes less than 100 milliseconds on interactive systems. The script in the above example will help you choose a good cost value for your hardware.

Note: Updates to supported algorithms by this function (or changes to the default one) must follow the following rules:

See Also