如何在laravel中比较两个加密(bcrypt)密码

mgdq6dx1  于 2023-02-05  发布在  其他
关注(0)|答案(5)|浏览(277)

如何比较两个bcrypt密码

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

还有

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

$pass1和$pass2都是“测试”的加密。
如何不使用文本'test'来检查是否相等

$hash1 = Hash::make('test');
$hash2 = Hash::make('test');

var_dump(Hash::check('test', $hash1) && Hash::check('test', $hash2));
pgky5nke

pgky5nke1#

if(Hash::check('plain-text-password',$cryptedpassword)) {
    // Right password
} else {
    // Wrong one
}
zynd9foi

zynd9foi2#

实际上,你不能直接将两个加密的bcrypt密码作为字符串进行比较,因为加密包含了盐,这使得每次的哈希值都不同。

vnjpjtjt

vnjpjtjt3#

您可以简单地使用Hash::check()方法,例如:

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

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

mkh04yzy

mkh04yzy4#

您可以尝试以下方法:
PHP Manual on crypt(参考:实施例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!!';
}
lvjbypge

lvjbypge5#

你可以比较哈希加密密码使用**Hash.
但是注意,在该方法中,第一值应该是
plain-text,第二值应该是bcrypt**。

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

相关问题