[英]How to check if a string is already hashed in Node.js using crypto
我正在开发一个允许用户更改密码的应用程序。
我正在使用 Node.js 和mongoose
和crypto
。
为了生成密码的哈希值,我已经连接到模型的 pre('save') 事件。 所以基本上当你使用它时:
var user = new User({password: '123'});
user.save(); // the password will be encrypted automatically
具有更改密码的能力,我需要找到一种方法,如果用户实际上已经更改了密码或修改了任何其他字段。 例子:
var user = User.findById('1234567', function(error, user){
user.name = 'Hello';
user.save(); // this will hash the password again, although it is not necessary
})
我正在使用名为crypto
的 Node.js 模块生成密码 hash:
crypto.pbkdf2(password, salt, 5000, 512, 'sha512', function(error, buffer){
this.mixins.callback.call(callback, [error, buffer.toString('base64')]);
}.bind(this));
有没有办法检查password
是否已经过哈希处理?
如果没有,我将再次生成 hash。
谢谢
这本质上是不好的方法。 只需调用字段password_hash
并始终显式传递哈希即可。 否则,在进行一些小的配置更改后,您可能会意外地开始存储未加密的密码。
如果允许使用任意字符串作为密码,通常也无法将哈希与未哈希的字符串区分开。
散列字符串具有 hash 算法标识符前缀。 您可以通过检查字符串是否以$2a$
或$2b$
开头来验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.