如何使用加密检查字符串是否已在 Node.js 中进行哈希处理

How to check if a string is already hashed in Node.js using crypto

我正在开发一个允许用户更改密码的应用程序。

我正在使用 Node.js 与 mongoosecrypto

为了生成密码的哈希值,我已经连接到模型的 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
})

我正在使用 Node.js 的名为 crypto 的模块来为密码生成哈希:

crypto.pbkdf2(password, salt, 5000, 512, 'sha512', function(error, buffer){

        this.mixins.callback.call(callback, [error, buffer.toString('base64')]);

}.bind(this));

有没有办法检查 password 是否已经经过哈希处理?
如果没有,我会重新生成哈希。

谢谢

这本质上是一种糟糕的方法。只需调用字段 password_hash 并始终显式传递哈希。否则您可能会在一些小的配置更改后意外地开始存储未散列的密码。

此外,如果您允许任意字符串作为密码,则通常无法区分散列和未散列的字符串。