Node.js 中的 crypto.pbkdf2Sync() 方法


crypto.pbkdf2Sync(),也稱為基於密碼的金鑰派生函式 2,提供了派生函式的同步實現。金鑰透過使用密碼、鹽和迭代次數指定的演算法的 Hmac 摘要派生。這將在同步過程中建立金鑰。

語法

crypto.createHmac(algorithm, key, [options])

引數

上述引數描述如下,−

  • password – 密碼,用於獲取請求位元組長度的金鑰。可能的值包括 string、DataView、Buffer 等型別。

  • salt – 類似於獲取金鑰的密碼。可能的值包括 string、DataView、Buffer 等型別。

  • iterations – 獲取所請求位元組長度的所需金鑰。它接受數值作為值。

  • keylen – 這是金鑰的請求位元組長度。它為數值型別。

  • digest – Hmac 演算法由此摘要值指定。預設值為“sha1”

示例

建立一個名為 pbkdf2Sync.js 的檔案,並複製以下程式碼段。建立檔案後,按照示例中所示使用以下命令執行此程式碼,−

node pbkdf2Sync.js

pbkdf2Sync.js

 現場演示

// crypto.pbkdf2() demo example

// Importing the crypto module
const crypto = require('crypto');

// Defining the pbkdf2 with the following options
const pbkdfKey = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
// Printing the derivedKey
console.log("key is: ",pbkdfKey.toString('hex'));

輸出

C:\home
ode>> node pbkdf2Sync.js key is: 3745e482c6e0ade35da10139e797157f4a5da669dad7d5da88ef87e47471cc47ed941c7ad618e8 27304f083f8707f12b7cfdd5f489b782f10cc269e3c08d59ae

示例

我們再看另一個示例。

 現場演示

// crypto.pbkdf2Sync () demo example

// Importing the crypto module
const crypto = require('crypto');

// Defining the pbkdf2Sync with the following options
const pbkdfKey = crypto.pbkdf2Sync('secret', 'salt', 100, 32, 'sha1');
// Printing the derivedKey
console.log("key is: ",pbkdfKey);
console.log("key(in hex) is: ",pbkdfKey.toString('hex'));
console.log("key(in base64) is: ",pbkdfKey.toString('base64'));

輸出

C:\home
ode>> node pbkdf2Sync.js key is: <Buffer b7 36 35 f7 c0 88 2e 1f c3 ba 6e 29 b1 4a f1 27 4d f8 48 28 b4 d1 8f cc 22 2e b5 74 45 5f 50 5d> key(in hex) is: b73635f7c0882e1fc3ba6e29b14af1274df84828b4d18fcc222eb574455f505d key(in base64) is: tzY198CILh/Dum4psUrxJ034SCi00Y/Mii61dEVfUF0=

更新於: 2021-05-20

974 次瀏覽

開始您的 職業

完成課程並獲得認證

開始該做
廣告
© . All rights reserved.