javascript 尝试在异步函数中使用 bcrypt 散列密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48799894/
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
Trying to hash a password using bcrypt inside an async function
提问by Modermo
Following on from this question.
从这个问题开始。
I feel like I'm almost there, but my incomplete understanding of async is preventing me from solving this. I'm basically trying to just hash a password using bcrypt and have decided to seperate out the hashPassword function so that I can potentially use it in other parts of the app.
我觉得我快到了,但是我对异步的不完整理解阻止了我解决这个问题。我基本上只是尝试使用 bcrypt 对密码进行哈希处理,并决定分离 hashPassword 函数,以便我可以在应用程序的其他部分使用它。
hashedPasswordkeeps returning undefined though...
hashedPassword虽然一直返回未定义...
userSchema.pre('save', async function (next) {
let user = this
const password = user.password;
const hashedPassword = await hashPassword(user);
user.password = hashedPassword
next()
})
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
return err;
}
return hash
});
return hashedPassword
}
回答by Akash Dathan
awaitdosent wait forbcrypt.hashbecausebcrypt.hashdoes not return a promise. Use the following method, which wrapsbcryptin a promise in order to useawait.
awaitdosent 等待bcrypt.hash因为bcrypt.hash不返回承诺。使用以下方法,该方法包装bcrypt在承诺中以便使用await.
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await new Promise((resolve, reject) => {
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) reject(err)
resolve(hash)
});
})
return hashedPassword
}
回答by umar882
By default, bcrypt.hash(password,10)will return as promise. please check here
默认情况下,bcrypt.hash(password,10)将作为承诺返回。请检查这里
Example:Run the code,
示例:运行代码,
var bcrypt= require('bcrypt');
let password = "12345";
var hashPassword = async function(){
console.log(bcrypt.hash(password,10));
var hashPwd = await bcrypt.hash(password,10);
console.log(hashPwd);
}
hashPassword();
Output:
输出:
Promise { <pending> }
bY5Oj329TeEh8weYpJA6EOE39AA/BXVFOEUn1YOFC.sf1chUi4H8i
When you use awaitinside the async function, it will wait untill it get resolved from the promise.
当您await在 async 函数内部使用时,它会一直等到它从承诺中得到解决。
回答by CodeLover
Hashing bcrypt asynchronously should be like this
异步散列bcrypt应该是这样的
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
throw err;
}
// Do whatever you like with the hash
});
If you are confused with sync and async. You need to read more about them. There are a lot of good articles out there.
如果您对同步和异步感到困惑。你需要阅读更多关于它们的信息。那里有很多好文章。
回答by SLIMANI Mohammed
use The method bcrypt.hashSync(), It is Synchronous out of the box.
使用方法 bcrypt.hashSync(),它是同步的开箱即用。
const hashedPassword = bcrypt.hashSync(password,saltRounds);

