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
。
哪裏,- cost:它是CPU或內存的成本參數。它是一個數字,並且必須是2的冪但大於1,默認情況下,其值為16384。
- blockSize:它是分配的塊大小的參數。它是一個數字,默認值為8。
- parallelization:它是並行化的參數。它是一個數字,默認值為1。
- N:這是成本的別名。它是一個數字,隻能定義兩者之一。
- r:它是blockSize的別名。它是一個數字,隻能定義兩者之一。
- p:它是並行化的別名。它是一個數字,隻能定義兩者之一。
- 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
相關用法
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM monochrome()用法及代碼示例
- Node.js GM equalize()用法及代碼示例
- Node.js GM enhance()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | crypto.scrypt() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。