异步或同步bcrypt功能,在使用的Node.js以生成散列? [英] Async or Sync bcrypt function to use in node.js in order to generate hashes?

查看:208
本文介绍了异步或同步bcrypt功能,在使用的Node.js以生成散列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力使认证模块为我node.js的项目呢?

I'm currently trying to make authentication module for my project in node.js?

我已经看到了使用bcrypt生成散列的一些例子,即

I've already seen some examples of using bcrypt to generate hashes, i.e.

https://github.com/bnoguchi/mongoose-auth/blob/master/lib/modules/password/plugin.js
https://github.com/Turbo87/locomotive-passport-boilerplate/blob/master/app/models/account.js

https://github.com/bnoguchi/mongoose-auth/blob/master/lib/modules/password/plugin.js https://github.com/Turbo87/locomotive-passport-boilerplate/blob/master/app/models/account.js

然而,由于某种原因,他们使用bcrypt.hashSync()函数。由于bcrypt是很好的,因为它是费时,不会是明智的使用异步函数,而不是为了不挡住code,即:

However, for some reason they are using bcrypt.hashSync() function. Since bcrypt is good because it's time-consuming, wouldn't it be wiser to use asynchronous function instead in order to not block the code, i.e:

User.virtual('password')
.get( function () {
    return this.hash;
})
.set( function (password) {
    bcrypt.hash('password', 10, function(err, hash) {
        this.hash = hash;
    });
});

能否请你解释我哪种方式更好,为什么?谢谢!

Could you please explain me which way is better and why? Thank you!

推荐答案

是的,你想使用异步版本,如果有可能让你没有密码哈希期间占用你的节点处理。在你可以引用源$ C ​​$ C的情况下,code使用的是同步的版本,因为它的内部采用的方法是同步因此笔者只好使用同步版本。

Yes, you'd want to use the async version if possible so you're not tying up your node processing during the password hash. In both source code cases you reference, the code is using the synchronous version because the method it's used within is synchronous so the author had no choice but to use the synchronous version.

这篇关于异步或同步bcrypt功能,在使用的Node.js以生成散列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆