password_verify
(PHP 5 >= 5.5.0)
password_verify — Verifies that a password matches a hash
Description
$password
, string $hash
)Verifies that the given hash matches the given password.
Note that password_hash() returns the algorithm, cost and salt as part of the returned hash. Therefore, all information that's needed to verify the hash is included in it. This allows the verify function to verify the hash without needing separate storage for the salt or algorithm information.
Parameters
-
password
-
The user's password.
-
hash
-
A hash created by password_hash().
Return Values
Returns TRUE
if the password and hash match, or FALSE
otherwise.
Examples
Example #1 password_verify() example
<?php
// See the password_hash() example to see where this came from.
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>
The above example will output:
Password is valid!
Коментарии
Imagine you are using an API between servers (where the password is stored in the API server) and you want to avoid passing the password in plain text via the API, or sending the full hash to the API client.
You can just receive the original salt and re-crypt the password on the API client, then you can send the recrypted password to the API server to compare them.
<?php
define('BLOWFISH_SALT_LENGTH', 23);
$pass = 'maduranma';
$original = password_hash($pass, PASSWORD_DEFAULT); // Equivalent to PASSWORD_BCRYPT
$salt = substr($original, 0, strrpos($original, '$') + BLOWFISH_SALT_LENGTH);
$recrypted = crypt($pass, $salt); // This can be done on the API client and sent to the API server
// So we can compare both:
var_dump($original === $recrypted); // Returns true, should be done on the API server
Imagine you are using an API between servers (where the password is stored in the API server) and you want to avoid passing the password in plain text via the API, or sending the full hash to the API client.
You can just receive the original salt and re-crypt the password on the API client, then you can send the recrypted password to the API server to compare them.
<?php
define('BLOWFISH_SALT_LENGTH', 23);
$pass = 'maduranma';
$original = password_hash($pass, PASSWORD_DEFAULT); // Equivalent to PASSWORD_BCRYPT
$salt = substr($original, 0, strrpos($original, '$') + BLOWFISH_SALT_LENGTH);
$recrypted = crypt($pass, $salt); // This can be done on the API client and sent to the API server
// So we can compare both:
var_dump($original === $recrypted); // Returns true, should be done on the API server