简体   繁体   English

NodeJS pbkdf2Sync密码具有从v0.12.7到v6.9.1的可移植性

[英]NodeJS pbkdf2Sync password hasing portability from v0.12.7 to v6.9.1

I have been working on porting an application written from node v0.12.7 to node v6.9.1. 我一直在努力将从节点v0.12.7编写的应用程序移植到节点v6.9.1。

We are using MEAN stack with all of them upgraded to their latest versions. 我们正在使用MEAN堆栈,所有这些都升级到了最新版本。

We have been able to upgrade everything except for one problem. 除了一个问题,我们已经能够升级所有内容。 We used pbkdf2Sync method (comes inbuilt with express) for hashing passwords like so: 我们使用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;
   }
};

In the latest versions, they have changed the encoding to utf8 and also changed the pbkdf2Sync to include a mandatory digest. 在最新版本中,他们已将编码更改为utf8,并且还更改了pbkdf2Sync以包含强制摘要。 I am not sure what else they have changed. 我不确定他们还改变了什么。

The problem: 问题:

The passwords hashed and stored in the mongo database using earlier version of node does not match with the password generated by hashPassword function after version upgrade. 使用早期版本的节点散列并存储在mongo数据库中的密码与版本升级后hashPassword函数生成的密码不匹配。

I have tried: 我试过了:

1) Specifying encoding string 1)指定编码字符串

2) Using buffers 2)使用缓冲区

3) Adding digest option as parameter 3)添加摘要选项作为参数

And I dont get the same hashed password with any of these. 我没有得到任何这些哈希密码。

I tried changing the hashPassword function with many combinations. 我尝试使用许多组合更改hashPassword函数。 One of the tries I made was this, but does not work. 我做的一个尝试就是这个,但是没有用。

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;
 }
};

A test case: 一个测试用例:

Password to hash: ramco@123 哈希密码:ramco @ 123

Salt: d\' \\\)aq **G\ 盐:d \' \\ u0001 \\ u0004 \\ u0012)aq ** G \\ u000f

Result I am supposed to get: kG6uCjSk87I7PrXMko+nS8Mz/78LMilXDMJZI0mzBgi75mBpi8hIkh3+B8CqpuYZdvvs5HWjcNthhhnUA89sCw== 结果我应该得到:kG6uCjSk87I7PrXMko + nS8Mz / 78LMilXDMJZI0mzBgi75mBpi8hIkh3 + B8CqpuYZdvvs5HWjcNthhhnUA89sCw ==

But I get some other string from the hashPassword function. 但我从hashPassword函数中得到了一些其他字符串。

I referred: 我提到:

The NodeJS commit made in git: https://github.com/nodejs/node/commit/b010c8716498dca398e61c388859fea92296feb3 在git中提交的NodeJS提交: https//github.com/nodejs/node/commit/b010c8716498dca398e61c388859fea92296feb3

The express commit made in git: https://github.com/meanjs/mean/commit/61f1a22c91ac15f06143ace6e540b334fa9e3bd6 在git中提交的快速提交: https//github.com/meanjs/mean/commit/61f1a22c91ac15f06143ace6e540b334fa9e3bd6

Crypto documentation: https://nodejs.org/api/crypto.html 加密文档: https//nodejs.org/api/crypto.html

How to store crypto pbkdf2 in mongoDB? 如何在mongoDB中存储crypto pbkdf2?

And lot of other sites and forums but did not help me. 许多其他网站和论坛,但没有帮助我。 Kindly help me if you can. 如果可以,请帮助我。

Thanks in advance. 提前致谢。

Looks like older versions of Node used SHA-1 as digest. 看起来旧版本的Node使用SHA-1作为摘要。 Also, you should pass in the salt as a binary buffer (I'm using the supplied salt string from your example, if you have stored the salt as Base64-encoded binary you can probably leave the base64 encoding that you're using as-is and only explicitly set the correct digest). 此外,您应该将salt作为二进制缓冲区传递(我使用您示例中提供的salt字符串,如果您将salt存储为Base64编码的二进制文件,则可以保留您正在使用的base64编码 -是,并且只显式设置正确的摘要)。

The following yields the expected result: 以下结果产生预期结果:

const crypto = require('crypto');

let password = 'ramco@123';
let salt     = `d\u001e'��\u0001\u0004\u0012)aq�**G\u000f`;

let x = crypto.pbkdf2Sync(password, new Buffer(salt, 'binary'), 10000, 64, 'sha1').toString('base64');

console.log(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM