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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35715755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:33:22  来源:igfitidea点击:

How to compare two encrypted(bcrypt) password in laravel

phplaravellaravel-5bcrypt

提问by Jija

How to compare two bcrypt password

如何比较两个bcrypt密码

$pass1 = 'y$ooPG9s1lcwUGYv1nqeyNcO0ccYJf8hlhm5dJXy7xoamvgiczXHB7S';

And

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

Both $pass1 & $pass2 are bcrypt for 'test'.

$pass1 和 $pass2 都是 'test' 的 bcrypt。

How I can check for equality. without using text 'test' like this

我如何检查相等性。不使用这样的文本“测试”

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

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

采纳答案by Joseph

You can't actually compare two encrypted bcrypt passwords to each other directly as strings because the encryption contains salt which makes the hashes different each time.

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

回答by Martin

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

回答by Rahul Reghunath

You can simply use Hash::check()method eg:

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

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

reference https://laravel.com/docs/5.5/hashing

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

回答by bharat

You can try this way:

你可以试试这个方法:

if (Hash::check('test', bcrypt('test'))) {
    return 'match!!';
}else{
    return 'not match!!';
}

回答by pankaj kumar

you can compare hash encrypt the password using Hash.

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

but note that in this method the first value should be plain-textand second bcryptvalue.

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

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