Node.js crypto.scryptSync( ) Function
Last Updated :
09 Apr, 2021
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
Similar Reads
Node.js crypto.sign() Function Cryptography is the process of converting plain text into unreadable which is hashed from text and vice-versa and the crypto.sign() is used to create signature of data. Syntax: crypto.sign(algorithm, data, key)Parameters: This function accepts the following parameters: algorithm: It is a string-ty
3 min read
Node.js crypto.setEngine() Function Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the
2 min read
Node.js crypto.verify() Function The crypto.verify() Â is a method of the inbuilt module of node.js crypto that is used to verify the signature of data that is hashed using different kinds of hashing functions Like SHA256 algorithm etc. Syntax: crypto.verify(algorithm, data, publicKey, signature) Parameters: algorithm:Â It is a stri
2 min read
Node.js crypto.randomUUID( ) Function The crypto.randomUUID() is  an inbuilt application programming interface of class Crypto within crypto module which is used to generate a random RFC 4122 Version 4 UUID. Syntax: const crypto.randomUUID([options]) Parameters: This function takes the disableEntropyCache as a parameter Return Value: Th
1 min read
Node.js crypto.hkdf() Function The crypto.hkdf() is  an inbuilt application programming interface of class Crypto within crypto module which is used to derive a key of the particular length. Syntax: const crypto.hkdf(digest, key, salt, info, keylen, callback) Parameters: This function takes the following arguments as the paramete
2 min read