password_verify

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

password_verify验证密码是否和散列值匹配

说明

password_verify(string $password, string $hash): bool

验证密码是否和指定的散列值匹配。password_verify()crypt() 兼容。因此,由 crypt() 创建的密码散列可以用于 password_verify() 一起使用。

注意 password_hash() 返回的散列包含了算法、 cost 和盐值。 因此,所有需要的信息都包含内。使得验证函数不需要储存额外盐值等信息即可验证散列。

时序攻击(timing attacks)对此函数不起作用。

参数

password

用户的密码。

hash

一个由 password_hash() 创建的散列值。

返回值

如果密码和散列值匹配则返回 true,否则返回 false

示例

示例 #1 password_verify() 示例

这只是简单的示例,如果需要,建议重新生成正确的密码;请参见 password_needs_rehash() 示例。

<?php
// 想知道以下字符从哪里来,可参见 password_hash() 示例
$hash = '$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a';

if (
password_verify('rasmuslerdorf', $hash)) {
echo
'Password is valid!';
} else {
echo
'Invalid password.';
}
?>

以上示例会输出:

Password is valid!

参见

add a note add a note

User Contributed Notes 5 notes

up
265
Anonymous
10 years ago
If you get incorrect false responses from password_verify when manually including the hash variable (eg. for testing) and you know it should be correct, make sure you are enclosing the hash variable in single quotes (') and not double quotes (").

PHP parses anything that starts with a $ inside double quotes as a variable:

<?php
// this will result in 'Invalid Password' as the hash is parsed into 3 variables of
// $2y, $07 and $BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq
// due to it being enclosed inside double quotes
$hash = "$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq";

// this will result in 'Password is valid' as variables are not parsed inside single quotes
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

if (
password_verify('rasmuslerdorf', $hash)) {
    echo
'Password is valid!';
} else {
    echo
'Invalid password.';
}
?>
up
50
Vasil Toshkov
11 years ago
This function can be used to verify hashes created with other functions like crypt(). For example:

<?php

$hash
= '$1$toHVx1uW$KIvW9yGZZSU/1YOidHeqJ/';

if (
password_verify('rasmuslerdorf', $hash)) {
    echo
'Password is valid!';
} else {
    echo
'Invalid password.';
}

// Output: Password is valid!

?>
up
25
chris at weeone dot de
8 years ago
The function password_verify() uses constant time. This makes it safe against timing attacks. Don't use crypt($password_database) === crypt($password_given_by_login), since there is no protection against timing attacks!

If you don't want to use password_verify(), then have a look at hash_equals(), which also runs a timing attack safe string comparison.
up
18
omidbahrami1990 at gmail dot com
6 years ago
This Is The Most Secure Way To Keep Your Password Safe With PHP 7 ,
Even When Your DataBase Has Been Hacked ,
It Will Be Almost Impossible To Retrieve Your Password .
--------------------------------------------------------
--- When A User Wants To Sign Up ---
1 ---> Get Input From User Which Is The User`s Password
1 ---> Hash The Password
2 ---> Store The Hashed Password In Your DataBase
--------------------------------------------------------
<?php
$hashed_password
= password_hash($_POST["password"],PASSWORD_DEFAULT);

// $_POST["password"] ---> Is The User`s Input
// $hashed_password ---> Is The Hashed Password You Can Store In Your DataBase
?>
--------------------------------------------------------
--- When A User Wants To Sign In ---
1 ---> Get Input From User Which Is The User`s Password
2 ---> Fetch The Hashed Password From Your Database
3 ---> Compare The User`s Input And The Hashed Password
--------------------------------------------------------
<?php
   
if(password_verify($_POST["password"],$hashed_password))
    echo
"Welcome";

    else
    echo
"Wrong Password";

// $_POST["password"] ---> Is The User`s Input
// $hashed_password ---> Is The Hashed Password You Have Fetched From DataBase
?>
up
-56
suit at rebell dot at
11 years ago
As Vasil Toshkov stated, password_verify() can be used to verify a password created by crypt() or password_hash()

That is because passwords created by password_hash() also use the C crypt sheme

If you want to verify older plain MD5-Hashes you just need to prefix them with $1$

See https://en.wikipedia.org/wiki/Crypt_(C) for more information.
To Top