当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js crypto.scrypt()用法及代码示例


crypto.scrypt()方法是加密模块的内置应用程序编程接口,用于启用异步scrypt的实现。其中,scrypt是基于密码的 key 派生函数。这样做的目的是要在计算上加上内存方面增加成本。因此,暴力攻击无法成功进行。

用法:

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

参数:此方法接受上述和以下所述的五个参数:



  • password:它可以保存字符串,Buffer,TypedArray或DataView类型的数据。
  • salt:它保存字符串,Buffer,TypedArray或DataView类型的数据。它必须尽可能唯一。此外,建议盐应该是随机的,并且长度至少为16个字节。
  • keylen:它是 key 的长度,必须是一个数字。
  • options:它是对象类型,具有七个参数,分别是cost, blockSize, parallelization, N, r, p,maxmem
    哪里,
    1. cost:它是CPU或内存的成本参数。它是一个数字,并且必须是2的幂但大于1,默认情况下,其值为16384。
    2. blockSize:它是分配的块大小的参数。它是一个数字,默认值为8。
    3. parallelization:它是并行化的参数。它是一个数字,默认值为1。
    4. N:这是成本的别名。它是一个数字,只能定义两者之一。
    5. r:它是blockSize的别名。它是一个数字,只能定义两者之一。
    6. p:它是并行化的别名。它是一个数字,只能定义两者之一。
    7. maxmem:它是要使用的内存的上限。这是一个数字,当128 * N * r(大约)大于maxmem时,可能会发生错误。默认值为(32 * 1024 * 1024)。
  • callback它是一个具有两个参数的函数,即err和派生 key 。

返回值:它返回一个缓冲区。

以下示例说明了Node.js中crypto.scrypt()方法的使用:

范例1:

// Node.js program to demonstrate the 
// crypto.scrypt() method 
  
// Including crypto module 
var crypto = require('crypto'); 
  
// Calling scrypt method with some of its parameter 
crypto.scrypt('GfG', 'ffdgsg', 32, (err, derivedKey) => { 
  
  if (err) throw err; 
  
  // Prints derived key as buffer 
  console.log("The derived key1 is:", derivedKey); 
}); 
  
// Calling scrypt method with the parameter N 
crypto.scrypt('GeeksforGeeks', 'tfytdx', 128, 
           { N:512 }, (err, derivedKey) => { 
  
  if (err) throw err; 
  
  // Prints derived key as buffer 
  console.log("The derived key2 is:", derivedKey); 
  console.log(); 
});

输出:

The derived key2 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 key1 is:<Buffer dd 47 ee 3e a8 2e
f2 5b eb 18 7d 35 1b fd f5 a8 e5 f5 38 ef a7 ff 05
53 1e 86 69 ad cd e8 89 76 >

范例2:

// Node.js program to demonstrate the 
// crypto.scrypt() method 
  
// Including crypto module 
var crypto = require('crypto'); 
  
// Defining salt as typed array 
const x = new Uint32Array(7); 
  
// Calling scrypt method with some of its parameter 
crypto.scrypt('yytunnd', x,  16, (err, derivedKey) => { 
  
  if (err) throw err; 
  
  // Prints derived key which is encoded 
  console.log("The derived key1 is:", 
            derivedKey.toString("ascii")); 
}); 
  
// Defining salt as data view 
const y = new DataView(new ArrayBuffer(5)); 
  
// Calling scrypt method with the parameter N 
crypto.scrypt('oksjdjdn', y, 16, { N:32 }, 
                (err, derivedKey) => { 
  
  if (err) throw err; 
  
  // Prints derived key after encoding 
  console.log("The derived key2 is:", 
            derivedKey.toString("base64")); 
  console.log(); 
});

输出:

The derived key2 is:6Gu0JKHDSHs0tkTuGYuQ7A==
The derived key1 is:G"@&H 
                           pVCD3                               
                                X%

参考: https://nodejs.org/api/crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | crypto.scrypt() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。