Open In App

Node.js crypto.scryptSync( ) Function

Last Updated : 09 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The crypto.scryptSync() is an inbuilt function which Provides a synchronous scrypt implementation in Node.js. scrypt is a password-based key derivation function. It is intended to be costly computationally plus memory-wise. So, the brute-force attacks are made unsuccessful.

Syntax:

crypto.scryptSync(password, salt, keylen[, options])

Parameter: This method accept five parameters as mentioned above and described below:

  • password: It can hold string, Buffer, TypedArray, or DataView type of data.
  • salt: It holds string, Buffer, TypedArray, or DataView type of data. It must be as unique as possible. Moreover, it is recommended that salt should be random and is at a minimum of 16 bytes long.
  • keylen: It denotes the length of the key, and it must be a number.
  • options:It is of type Object, and it has seven parameters namely cost, blockSize, parallelization, N, r, p, and maxmem.

Return Value: It returns a buffer.

Below examples illustrate the use of crypto.scryptSync() method in Node.js:

Example 1:

app.js




// Example of  crypto.scryptSync() Method
  
// Including crypto module in the app.js. 
import crypto from 'crypto'
  
// Calling scryptSync() method with some of its parameter 
const keyExample = crypto.scryptSync('GeeksforGeeks'
                                     'gfggfg', 32);
  
// Prints result key as buffer 
console.log("The key in the form of buffer is:",keyExample);  
  
  
// Calling scryptSync() method with the parameter N 
const keyExample2 = crypto.scryptSync('GFG_ARTICLE', 'GFGGFG', 64, { N: 512 });
  
// Prints result key as buffer 
console.log("The key in the form of buffer is :",keyExample2);


node app.js

Output:

The key in the form of buffer is : <Buffer 
d9 9d b7 7e 7c 25 cb 39 db 3b 16 6b b8 47 73 43 4e 
96 72 9c 02 b3 55 7b 7d 66 f5 54 e6 e3 a0 e6>

The key in the form of buffer is : <Buffer 
26 c4 91 3f a7 03 b8 30 7a b5 bf a6 fe de 34 aa 46 
8c b5 dd b6 0d ce 5a 5e 03 91 a1 7c 7d ba d0 5b d6 
62 8e 11 3a 45 f6 41 a2 be b2 39 c9 57 5a 78 55 ... 
14 more bytes>

Example 2:

app.js




// Example of  crypto.scryptSync() Method
  
// Including crypto module in the app.js. 
import crypto from 'crypto'
  
// Defining salt as typed array 
 const arr = new Float64Array(8.0)
  
// Calling scryptSync() method with some of its parameter 
const keyExample = crypto.scryptSync('gghhgh', arr, 16);
  
// Prints result key as buffer 
console.log("The key in the form of ASCII is :"
             ,keyExample.toString("ascii")); 
  
  
// Defining salt as data view 
const arr1= new DataView(new ArrayBuffer(17)); 
  
// Calling scryptSync() method with the parameter N 
const keyExample2 = crypto.scryptSync('jbjbb', arr1, 16, { N: 16 });
  
// Prints result key as buffer 
console.log("The key in the form of hex is :",keyExample2.toString('hex'));


node app.js

Output:

The key in the form of ASCII is: k&q0Y4EQ_72
The key in the form of hex is : 
29dcb56a3d91f7be66de7444bc7ac605


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads