PHP.nl

password_hash

password_hash

Creates a password hash

string **password_hash** string $password  $algo array $options
creates a new password hash using a strong one-way hashing

algorithm. password_hash

The following algorithms are currently supported:

    • Use the bcrypt algorithm (default as of PHP 5.5.0). Note that this constant is designed to change over time as new and stronger algorithms are added to PHP. For that reason, the length of the result from using this identifier can change over time. Therefore, it is recommended to store the result in a database column that can expand beyond 60 bytes (255 bytes would be a good choice). PASSWORD_DEFAULT
    • Use the bcrypt algorithm to create the hash. This will produce a standard compatible hash using the identifier. PASSWORD_BCRYPT``crypt``$2y$
    • Use the Argon2i hashing algorithm to create the hash. This algorithm is only available if PHP has been compiled with Argon2 support. PASSWORD_ARGON2I
    • Use the Argon2id hashing algorithm to create the hash. This algorithm is only available if PHP has been compiled with Argon2 support. PASSWORD_ARGON2ID

    Supported options for : PASSWORD_BCRYPT

    • () - to manually provide a salt to use when hashing the password. Note that this will override and prevent a salt from being automatically generated. salt``string If omitted, a random salt will be generated by for each password hashed. This is the intended mode of operation. password_hash > Waarschuwing: > The salt option is deprecated. It is now > preferred to simply use the salt that is generated by default. > As of PHP 8.0.0, an explicitly given salt is ignored.
  • () - which denotes the algorithmic cost that should be used. Examples of these values can be found on the page. cost``int``crypt If omitted, a default value of will be used. This is a good baseline cost, but it should be adjusted depending on hardware used. 12

    Supported options for and : PASSWORD_ARGON2I``PASSWORD_ARGON2ID

    • () - Maximum memory (in kibibytes) that may be used to compute the Argon2 hash. Defaults to . memory_cost``int``PASSWORD_ARGON2_DEFAULT_MEMORY_COST
  • () - Maximum amount of time it may take to compute the Argon2 hash. Defaults to . time_cost``int``PASSWORD_ARGON2_DEFAULT_TIME_COST

  • () - Number of threads to use for computing the Argon2 hash. Defaults to . threads``int``PASSWORD_ARGON2_DEFAULT_THREADS > Waarschuwing: > Only available when PHP uses libargon2, not with libsodium implementation.

passwordpassword.parameter.password

Let op: > Using the as the algorithm, will result in the parameter being truncated to a maximum length of 72 bytes. PASSWORD_BCRYPT``password

algopassword.parameter.algo

optionspassword.parameter.options

Returns the hashed password.

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 function to verify the hash without needing separate storage for the salt or algorithm information. password_verify

Voorbeeld: example

<?php
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT);
?>
$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.

Voorbeeld: example setting cost manually

<?php
$options = [
    // Increase the bcrypt cost from 12 to 13.
    'cost' => 13,
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
?>
$2y$13$xeDfQumlmdm0Sco.4qmH1OGfUUmOcuRmfae0dPJhjX1Bq0yYhqbNi

Voorbeeld: example finding a good cost

This code will benchmark the machine to determine how high of a cost can be used without deteriorating user experience. It is recommended to set the highest cost that does not slow down other operations the machine needs to perform. 11 is a good baseline, and more is better if the machine is fast enough. The code below aims for ≤ 350 milliseconds stretching time, which is an appropriate delay for systems handling interactive logins.

<?php
$timeTarget = 0.350; // 350 milliseconds

$cost = 11;
do {
    $cost++;
    $start = microtime(true);
    password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
    $end = microtime(true);
} while (($end - $start) < $timeTarget);

echo "Appropriate Cost Found: " . $cost - 1;
?>
Appropriate Cost Found: 13

Voorbeeld: example using Argon2i

<?php
echo 'Argon2i hash: ' . password_hash('rasmuslerdorf', PASSWORD_ARGON2I);
?>
Argon2i hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0

Let op: > It is strongly recommended not to provide an explicit salt for this function. A secure salt will automatically be created if no salt is specified.

As noted above, providing the  option in PHP 7.0.0
will generate a deprecation warning. Support for providing a salt explicitly
has been removed in PHP 8.0.0.

salt

Opmerking: > It is recommended to test this function on the machine used, adjusting the cost parameter(s) so that execution of the function takes less than 350 milliseconds for interactive logins. The script in the above example will help choosing an appropriate bcrypt cost for the given machine.

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

  • Any new algorithm must be in core for at least 1 full release of PHP prior to becoming default. So if, for example, a new algorithm is added in 7.5.5, it would not be eligible for default until 7.7 (since 7.6 would be the first full release). But if a different algorithm was added in 7.6.0, it would also be eligible for default at 7.7.0.
  • The default should only change in a full release (7.3.0, 8.0.0, etc) and not in a revision release. The only exception to this is in an emergency when a critical security flaw is found in the current default.

password_verify``password_needs_rehash``crypt``sodium_crypto_pwhash_str