【发布时间】:2016-10-27 17:42:17
【问题描述】:
我一直致力于将从节点 v0.12.7 编写的应用程序移植到节点 v6.9.1。
我们正在使用 MEAN 堆栈,所有堆栈都升级到最新版本。
我们已经能够升级所有东西,除了一个问题。我们使用 pbkdf2Sync 方法(内置 express)来散列密码,如下所示:
/**
* Hook a pre save method to hash the password
*/
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6) {
this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
this.password = this.hashPassword(this.password);
}
next();
});
/**
* Create instance method for hashing a password
*/
UserSchema.methods.hashPassword = function(password) {
if (this.salt && password) {
return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
} else {
return password;
}
};
在最新版本中,他们将编码更改为 utf8,并更改了 pbkdf2Sync 以包含强制摘要。我不确定他们还改变了什么。
问题:
使用node早期版本哈希存储在mongo数据库中的密码与版本升级后hashPassword函数生成的密码不匹配。
我试过了:
1) 指定编码字符串
2) 使用缓冲区
3) 添加摘要选项作为参数
而且我没有得到与其中任何一个相同的哈希密码。
我尝试使用多种组合更改 hashPassword 函数。我做过的尝试之一就是这样,但不起作用。
UserSchema.methods.hashPassword = function (password) {
if (this.salt && password) {
return crypto.pbkdf2Sync(password, new Buffer(this.salt, 'base64').toString('binary'), 10000, 64, 'SHA1').toString('base64');
} else {
return password;
}
};
一个测试用例:
哈希密码:ramco@123
盐:d\u001e'��\u0001\u0004\u0012)aq�**G\u000f
我应该得到的结果:kG6uCjSk87I7PrXMko+nS8Mz/78LMilXDMJZI0mzBgi75mBpi8hIkh3+B8CqpuYZdvvs5HWjcNthhhnUA89sCw==
但是我从 hashPassword 函数中得到了一些其他字符串。
我提到了:
在 git 中提交的 NodeJS 提交: https://github.com/nodejs/node/commit/b010c8716498dca398e61c388859fea92296feb3
在 git 中的快速提交: https://github.com/meanjs/mean/commit/61f1a22c91ac15f06143ace6e540b334fa9e3bd6
加密文档: https://nodejs.org/api/crypto.html
How to store crypto pbkdf2 in mongoDB?
还有很多其他网站和论坛,但对我没有帮助。如果可以的话,请帮助我。
提前致谢。
【问题讨论】:
标签: node.js mongodb hash pbkdf2