Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
N – This parameter is an alias for cost and can be used in its place. Only one can be defined at a time.
r – This parameter is an alias for blockSize and similarly only one can be defined at a time.
p – 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\node>> node scrypt.js The derived key(2) is:The derived key(1) is:
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\node>> node scrypt.js The derived key(2) is: PBYDRlgayLVGjC8z3YUcSQ== The derived key(1) is:
