如何在拉拉维尔中比较两个加密(bcrypt)密码


How to compare two encrypted(bcrypt) password in laravel

如何比较两个密码

$pass1 = '$2y$10$ooPG9s1lcwUGYv1nqeyNcO0ccYJf8hlhm5dJXy7xoamvgiczXHB7S';

$pass2 = '$2y$10$QRgaiS6bpATKKQeT22zGKuHq.edDfXQc2.4B3v.zaN.GtGwoyQuMy';

$pass 1 和 $pass 2 都是"测试"的 bcrypt。

我如何检查平等性。 不使用这样的文本"测试"

$hash1 = Hash::make('test');
$hash2 = Hash::make('test');
var_dump(Hash::check('test', $hash1) && Hash::check('test', $hash2));
if(Hash::check('plain-text-password',$cryptedpassword)) {
    // Right password
} else {
    // Wrong one
}

您实际上无法将两个加密的bcrypt密码作为字符串直接相互比较,因为加密包含盐,这使得每次的哈希都不同。

你可以

简单地使用Hash::check()方法例如:

if(Hash::check('plain-text', $hashedPassword)) {
    return true;
}

参考 https://laravel.com/docs/5.5/hashing

你可以试试这种方式:

关于地穴的PHP手册(参考:示例1)

<?php
// 1. for compare two crypted string
// ----------
// let the salt be automatically generated; not recommended
$hashed_password = crypt('mypassword');
/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) {
   echo "Password verified!";
}
// 2. for compare with normal text
// ----------
if (Hash::check('test', bcrypt('test'))) {
    return 'match!!';
}else{
    return 'not match!!';
}

您可以使用Hash比较哈希加密密码。

但请注意,在此方法中第一个值应为plain-text值,第二个值应bcrypt值。

Hash::check('test', bcrypt('test'))