laravel 在 jquery 或 javascript 中使用 bcrypt()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44622813/
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
use bcrypt() in jquery or javascript
提问by nikhil
this code is written in validation.js to validate the all laravel change password forms dynamically
这段代码写在validation.js 中以动态验证所有laravel 更改密码表单
$.validator.addMethod("matchp", function(value, element)
{
var dbpass = $("#old").val();
// #old value is fetch from database in type=hidden in frontend and its in bcrypt format.
var txtpass = bcrypt($("#oldpass").val());
// #oldpass value is fetch from frontend its user value and it sholud convert in bcrypt format.
// So that we can compare it to verify the old password while changing the old password.
// Check for equality with the password inputs
if (dbpass != txtpass ) {
return false;
} else {
return true;
}
}, "Your Passwords Must Match");
回答by Rohan Kumar
Bcryptfunction means an algorithm which gives different hash everytime for a unique string with some different salt. So it is not possible to validate it by using any JS Validation Plugin. Of course, you can check different input field values without using any hashing technique, like in the same way when validating password and confirm password fields.
Bcrypt函数是指一种算法,它每次为具有不同盐的唯一字符串提供不同的哈希值。因此无法使用任何 JS 验证插件对其进行验证。当然,您可以在不使用任何散列技术的情况下检查不同的输入字段值,就像验证密码和确认密码字段时一样。
If you are really required to use bcrypt at client-side, use a static salt.
如果您确实需要在客户端使用 bcrypt,请使用静态盐。
Updated,if you are using @Nevins-b JavaScript Bcryptplugin then you can do it by using same salt to validate different fields.
更新,如果您使用@Nevins-b JavaScript Bcrypt插件,那么您可以使用相同的盐来验证不同的字段。
Hope this is satisfactory to your question.
希望这对您的问题满意。
回答by Diego Poveda
That won't work because Laravel passwords are also salted.
这是行不通的,因为 Laravel 密码也被加盐了。
What I would do is
我会做的是
- Pass #oldpass un-encrypted to your back-end using ajax
On your back end compare the #oldpass with the current password using
Hash::check
if(Hash::check($request->oldpass, Auth::user()->password)){ return true;
}else{ return false; }
- 使用 ajax 将未加密的 #oldpass 传递到您的后端
在您的后端使用 #oldpass 与当前密码进行比较
Hash::check
if(Hash::check($request->oldpass, Auth::user()->password)){ return true;
}else{ return false; }
Dont forget to add the facades to the top of your Laravel controller
不要忘记将外墙添加到 Laravel 控制器的顶部
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;