Node.js crypto.scryptSync( )函数
crypto.scryptSync()是一个内置函数,它在 Node.js 中提供同步 scrypt 实现。 scrypt 是一个基于密码的密钥派生函数。它的目的是在计算上加上内存方面的成本很高。因此,蛮力攻击是不成功的。
句法:
crypto.scryptSync(password, salt, keylen[, options])
参数:此方法接受上述五个参数,如下所述:
- 密码:它可以保存字符串、 Buffer 、 TypedArray 或 DataView 类型的数据。
- salt:它保存字符串、Buffer、TypedArray 或 DataView 类型的数据。它必须尽可能独特。此外,建议 salt 应该是随机的,并且长度至少为 16 个字节。
- keylen:表示密钥的长度,必须是数字。
- options: Object类型,有cost、blockSize、parallelization、N、r、p、maxmem七个参数。
返回值:它返回一个缓冲区。
下面的示例说明了在 Node.js 中使用 crypto.scryptSync() 方法:
示例 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);
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
输出:
The key in the form of buffer is :
The key in the form of buffer is :
示例 2:
应用程序.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
输出:
The key in the form of ASCII is: k&q0Y4EQ_72
The key in the form of hex is :
29dcb56a3d91f7be66de7444bc7ac605