D.Shah
D.Shah

Reputation: 55

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. 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. 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?

Upvotes: 5

Views: 2643

Answers (1)

Paul Pritchard
Paul Pritchard

Reputation: 717

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. Up until Node 10, if digest wasn't provided then 'sha1' was used. But Node 10 requires it. Finding that out resolved my near as damn it identical message to the one you were getting.

Upvotes: 2

Related Questions