crypto.pbkdf2() Method in Node.js

The crypto.pbkdf2(), also known as Password-Based Key Derivation function, provides an asynchronous implementation of the derivative function. A key is derived by using the Hmac digest of a specified algorithm from password, salt and iterations
crypto.createHmac(algorithm, key, [options])
The above parameters are described as below −
password – Password defined for getting key of the requested byte length. Possible values are of type string, DataView, Buffer, etc.
password – Password defined for getting key of the requested byte length. Possible values are of type string, DataView, Buffer, etc.
salt – Similar to password for getting the key. Possible values are of type string, DataView, Buffer, etc.
salt – Similar to password for getting the key. Possible values are of type string, DataView, Buffer, etc.
iterations – Getting the desired key of requested byte length. It accepts the value as number.
iterations – Getting the desired key of requested byte length. It accepts the value as number.
keylen – This is the requested byte length of the key. It is of type number.
keylen – This is the requested byte length of the key. It is of type number.
digest – The Hmac algorithm is specified by this digest value. Default value is 'sha1'
digest – The Hmac algorithm is specified by this digest value. Default value is 'sha1'
callback – If any error occurs in the async mode, it will be handled in the callback
callback – If any error occurs in the async mode, it will be handled in the callback
Create a file with name – pbkdf2.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −
node pbkdf2.js
pbkdf2.js
 Live Demo
// crypto.pbkdf2() demo example

// Importing the crypto module
const crypto = require('crypto');

// Defining the pbkdf2 with the following options
crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key
   console.log("Key Derived: ",derivedKey.toString('hex'));
});
C:\home\node>> node pbkdf2.js
Key Derived:
3745e482c6e0ade35da10139e797157f4a5da669dad7d5da88ef87e47471cc47ed941c7ad618e8
27304f083f8707f12b7cfdd5f489b782f10cc269e3c08d59ae
Let's take a look at one more example.
 Live Demo
// crypto.pbkdf2() demo example

// Importing the crypto module
const crypto = require('crypto');

// Defining the pbkdf2 with the following options
crypto.pbkdf2('secret', 'salt', 100, 64, 'sha1', (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key
   console.log("Key Derived: ",derivedKey);
   console.log("Key Derived in hex: ",derivedKey.toString('hex'));
   console.log("Key Derived in base64: ",derivedKey.toString('base64'));
});
C:\home\node>> node pbkdf2.js
Key Derived: <Buffer b7 36 35 f7 c0 88 2e 1f c3 ba 6e 29 b1 4a f1 27 4d f8 48 28 b4 d1 8f cc 22 2e b5 74 45 5f 50 5d 3d 23 19 13 2d 84 e1 91 a7 83 e2 00 73 4e 37 4a 24 b6 ... >
Key Derived in hex:
b73635f7c0882e1fc3ba6e29b14af1274df84828b4d18fcc222eb574455f505d3d2319132d84e1
91a783e200734e374a24b62cfab65dfb5e9dc28ae147072419
Key Derived in base64:
tzY198CILh/Dum4psUrxJ034SCi00Y/MIi61dEVfUF09IxkTLYThkaeD4gBzTjdKJLYs+rZd+16dwo
rhRwckGQ==