我正在尝试将 crypto.pbkdf2 添加到我的系统中,但在使用 mocha 测试各种功能时,我一直返回摘要问题。我的散列方法代码是:
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);
};
我已尝试将摘要 ('sha1') 更改为许多内容,包括 'shah256'、'null' 或摘要。但是我的测试仍然失败并出现错误:
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)
如何解决我遇到的错误?
最佳答案
你现在可能已经解决了这个问题,但以防万一其他人也有这个问题 - 就像我一样:
函数 crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)
的功能在 Node 6 中发生了变化,使 digest 成为强制性的。在 Node 10 之前,如果未提供 digest,则使用“sha1”。但是 Node 10 需要它。
发现这一点解决了我几乎与你收到的完全相同的消息。
关于javascript - 如何修复 node.js 上的 crypto.pbkdf2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54446260/