crypto.scryptSync()是一個內置函數,可在Node.js中提供同步scrypt實現。 scrypt是基於密碼的 key 派生函數。這樣做的目的是要在計算上加上在內存方麵增加成本。因此,暴力攻擊無法成功進行。
用法:
crypto.scryptSync(password, salt, keylen[, options])
參數:此方法接受上述和以下所述的五個參數:
- password:它可以保存字符串,Buffer,TypedArray或DataView類型的數據。
- salt:它保存字符串,Buffer,TypedArray或DataView類型的數據。它必須盡可能唯一。此外,建議鹽應該是隨機的,並且至少應為16個字節長。
- keylen:它表示 key 的長度,並且必須是數字。
- 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);
node app.js
輸出:
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>
範例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
輸出:
The key in the form of ASCII is:k&q0Y4EQ_72 The key in the form of hex is: 29dcb56a3d91f7be66de7444bc7ac605
相關用法
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
- Node.js GM paint()用法及代碼示例
- Node.js GM orderedDither()用法及代碼示例
- Node.js GM roll()用法及代碼示例
- Node.js GM segment()用法及代碼示例
- Node.js GM quality()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
注:本文由純淨天空篩選整理自_sh_pallavi大神的英文原創作品 Node.js crypto.scryptSync() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。