What "digest" argument should I provide?

2.4k views Asked by At

I used to have the following code in the login and register function:

var crypto = require('crypto');
... ...
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');

And I have already registered users.

Yesterday, I updated npm, and now it shows an error:

events.js:182
      throw er; // Unhandled 'error' event
      ^

TypeError: The "digest" argument is required and must not be undefined
    at pbkdf2 (crypto.js:635:11)
    at Object.exports.pbkdf2Sync (crypto.js:628:10)
    at model.UserSchema.methods.validPassword (/opt/myapp/models/Users.js:35:23)
    at /opt/myapp/config/passport.js:16:23
    at model.Query.<anonymous> (/opt/myapp/node_modules/mongoose/lib/model.js:3745:16)
    at /opt/myapp/node_modules/kareem/index.js:277:21
    at /opt/myapp/node_modules/kareem/index.js:131:16
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

So does anyone know what digest argument I should provide to crypto.pbkdf2Sync? I hope the registered user/password are still valid.

1

There are 1 answers

4
Alexandru Olaru On

From node.js documentation for crypto.pbkdf2Sync:

const crypto = require('crypto');
const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
console.log(key.toString('hex'));  // '3745e48...aa39b34'

You also need to specify the encoding algorithm sha512 this is the digest parameter see the last argument in the documentation.