简体   繁体   English

如何修复Node.js上的crypto.pbkdf2错误

[英]How to fix crypto.pbkdf2 error on node.js

I'm attempting to add the crypto.pbkdf2 to my system and I keep returning a digest issue when using mocha to test the various functionalities. 我试图将crypto.pbkdf2添加到我的系统中,并且在使用Mocha测试各种功能时,我不断返回摘要问题。 My code for the hash method is: 我的哈希方法代码是:

Account.prototype.hashPassword = function (password, salt, callback) {
// we use pbkdf2 to hash and iterate 10k times by default 
 var iterations = 10000,
    keylen = 64; //64 bit.
this.crypto.pbkdf2(password, salt, iterations, keylen,'sha1', callback);
};

I've tried to change the digest ('sha1') to many things, including 'shah256', 'null' or digest. 我试图将摘要('sha1')更改为许多内容,包括'shah256','null'或摘要。 But my tests are still failing with the error: 但是我的测试仍然失败,并显示以下错误:

 TypeError [ERR_INVALID_ARG_TYPE]: The "digest" argument must be one of type string or null. Received type undefined
  at check (internal/crypto/pbkdf2.js:56:13)
  at Object.pbkdf2Sync (internal/crypto/pbkdf2.js:45:5)
  at UserMock.seedUsers (test\user-mock.js:32:39)
  at Context.<anonymous> (test\account-test.js:296:27)

How do I solve the error I am facing? 如何解决我面临的错误?

You have probably fixed this by now, but just in case someone else also has this problem - as I did: The functionality of the function crypto.pbkdf2Sync(password, salt, iterations, keylen, digest) changed in Node 6 to make digest compulsory. 您可能现在已经解决了这个问题,但是以防万一其他人也遇到了这个问题-就像我所做的那样:在节点6中更改了crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)函数的功能,使其必须强制进行摘要 Up until Node 10, if digest wasn't provided then 'sha1' was used. 直到节点10,如果未提供摘要 ,则使用“ sha1”。 But Node 10 requires it. 但是节点10需要它。 Finding that out resolved my near as damn it identical message to the one you were getting. 发现这一点后,我的想法和我得到的信息完全一样。

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

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