简体   繁体   English

BCrypt错误非法arguments:字符串,object

[英]BCrypt error Illegal arguments: string, object

I am developing a NodeJS and ReactJS based web application in which I am trying to hash the passwords in multiple routes, one for registration and other for changing password.我正在开发一个基于 NodeJS 和 ReactJS 的 web 应用程序,我正在尝试 hash 多个路径中的密码,一个用于注册,另一个用于更改密码。

For this purpose, I am generating the salt outside both routes so they both utilize the same salt like this:为此,我在两条路线之外生成盐,因此它们都使用相同的盐,如下所示:

const salt = bcrypt.genSalt(10);

Now inside the routes, the has is being generated like this:现在在路线内部,has 正在生成,如下所示:

user.password = await bcrypt.hash(newPassword, salt);

But when I run this, it gives the following console error:但是当我运行它时,它会出现以下控制台错误:

Illegal arguments: string, object非法 arguments:字符串,object

The hashing operation was working fine when I was generating the salt inside the individual routes.当我在各个路由中生成盐时,散列操作工作正常。 What could be the reason?可能是什么原因?

In case anyone else runs into this.以防其他人遇到这种情况。

Don't forget to await the genSalt function like so:不要忘记等待 genSalt function 像这样:

const salt = await bcrypt.genSalt(10);

The error: Illegal arguments: string, object tries to explain that one of the arguments passed to the hash function is invalid, since it's type is invalid. The error: Illegal arguments: string, object tries to explain that one of the arguments passed to the hash function is invalid, since it's type is invalid.

In this case it's the second argument (salt) which expects a string/number but receives an object (the promise object that's returned if you don't await ) .在这种情况下,它是第二个参数(盐) ,它需要一个字符串/数字,但接收一个 object (如果您不await ,则返回 promise object)

Hope that helps希望有帮助

I'm guessing you're calling bcrypt.hash like this我猜你正在像这样调用bcrypt.hash

await bcrypt.hash(newPassword, 10);

This means that you're passing the number of rounds, not a salt.这意味着您传递的轮数,而不是盐。 If you want to generate the salt outside of the call you can do something like this如果你想在通话之外生成盐,你可以做这样的事情

const salt = bcrypt.genSaltSync(10);

And then use it in bcrypt.hash .然后在bcrypt.hash中使用它。

looking at the docs, i believe the error is caused by the call back function missing in your genSalt().. genSaltSync() works well without the call back..查看文档,我相信该错误是由您的 genSalt() 中缺少的回调 function 引起的。genSaltSync() 在没有回调的情况下运行良好。

If it helps anyone out there, I've had the same issue but my genSalt code was fine.如果它可以帮助那里的任何人,我也有同样的问题,但我的genSalt代码很好。

My problem was that I had some users whose passwords were uninitialized, so no salt had been generated and it was the bcrypt.compare function that was failing because it was trying to compare to NULL我的问题是我有一些用户的密码未初始化,所以没有生成盐,它是bcrypt.compare function 失败,因为它试图与NULL进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:非法参数:未定义,MongoDB 中 Object.bcrypt.hashSync 处的字符串 - Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync in MongoDB Bcrypt 错误:非法参数字符串未识别 - Bcrypt error: illegal arguments String unidentified Bcrypt 错误:非法参数:字符串,未定义 - Bcrypt error: Illegal arguments: string, undefined bcrypt 错误:非法 arguments:字符串,未定义 - bcrypt Error: Illegal arguments: string, undefined 不断收到“非法 arguments:未定义,Object.bcrypt.hashSync 处的字符串” - keeps getting "Illegal arguments: undefined, string at Object.bcrypt.hashSync" Bcrypt 错误:错误:非法回调:NodeJS 中的字符串 - Bcrypt error: Error: Illegal callback: string in NodeJS 错误:非法参数:未定义,字符串 - Error: Illegal arguments: undefined, string “UnhandledPromiseRejectionWarning:错误:非法 arguments:未定义,字符串” - “UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string” 为什么此错误显示“非法 arguments:未定义,字符串”? - Why is this error displaying 'Illegal arguments: undefined, string'? Bcrypt 错误:非法参数:未定义,通过前端发送发布请求时的数字 - Bcrypt Error: Illegal arguments: undefined, number when sending post request through frontend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM