crypto.scrypt() Method in Node.js


The crypto.scrypt() method provides an asynchronous implementation for scrypt method. The scrypt can be defined as a password-based key derivation function that protects the system from the brute-force attacks and makes it unawarding. But the script function is expensive computationally as well as memory-wise.

Syntax

crypto.scrypt(password, salt, keylen, [options], [callback])

Parameters

The above parameters are described as below −

  • password – The password field for the scrypt required to decode entry. It can be a string, object, TypedArray, etc.

  • salt – This value should be as unique as possible. This is mainly used for encrypting the data. The minimum suggested length of salt is 16 bytes.

  • keylen – This parameter defined the length of the key and should be a number.

  • options

    • cost – This is the cost of the cpu for each memory taken. This value should be a power of 2 greater than 1. Default value is 16384.

    • blockSize – This parameter defined the value for each block size. Default value is 8.

    • parallelization – This parameter defined the parallelization parameter. Default value is 1.

    • – This parameter is an alias for cost and can be used in its place. Only one can be defined at a time.

    • – This parameter is an alias for blockSize and similarly only one can be defined at a time.

    • – Alias for parallelization. Only one can be defined.

    • maxmem – This is memory upper bound value. An error is thrown when 128*N*r > maxmem. Default value is 32*1024*1024.

  • callback – This function is called when an error is thrown if you want to handle it.

Example

Create a file with name – scrypt.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 scrypt.js

scrypt.js

// Node.js program to demonstrate the flow of crypto.scrypt() method

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

// Calling the scrypt() method with the below parameters
crypto.scrypt('tutorialspoint', 'asdfghjkl', 32, (err, derivedKey) => {

if (err) throw err;

// Prints the derived key as a buffer value
console.log("The derived key(1) is:", derivedKey);
});

// Calling the scrypt() method with the cost option
crypto.scrypt('GeeksforGeeks', 'tfytdx', 128,
   { N: 512 }, (err, derivedKey) => {

if (err) throw err;

// Prints the derived key as a buffer value
console.log("The derived key(2) is:", derivedKey);
console.log();
});

Output

C:\home
ode>> node scrypt.js The derived key(2) is: <Buffer b3 f8 72 5f 58 df 98 d9 c0 8a ba 0c 2c 50 85 b1 76 de 39 35 40 27 7d 57 f1 6a a1 07 54 dc c9 63 65 32 f2 db 29 95 dc ee 0b 9f e3 d5 0a 9e 3a d0 f6 b4 ... > The derived key(1) is: <Buffer ae 50 38 61 17 f7 11 51 e4 50 63 3c 2a 9c ec f0 46 42 a6 ca 04 78 67 05 c8 8c 0c 69 00 c3 03 7f>

Example

Let's take a look at one more example.

// Node.js program to demonstrate the flow of crypto.scrypt() method

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

// Initializing the value of salt as a typedArray
const salt = new Uint32Array(7);

// Using the scrypt() method with below parameters
crypto.scrypt('WelcomeTutorialspoint', salt, 16, (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key in encoded string format
   console.log("The derived key(1) is:",
      derivedKey.toString("ascii"));
});

// Initialising the value of salt as a DataView
const newSalt = new DataView(new ArrayBuffer(5));

// Using the script() method with cost parameter
crypto.scrypt('HelloTutorialspoint', newSalt, 16, { N: 32 }, (err, derivedKey) => {
   if (err) throw err;
   // Printing the derived key in encoded string format
   console.log("The derived key(2) is:",
      derivedKey.toString("base64"));
});

Output

C:\home
ode>> node scrypt.js The derived key(2) is: PBYDRlgayLVGjC8z3YUcSQ== The derived key(1) is: <Uu0allCb,d?,Z5_

Updated on: 20-May-2021

540 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements