當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js scrypto.hkdf()用法及代碼示例

crypto.hkdf()是加密模塊內的Crypto類的內置應用程序編程接口,用於派生特定長度的 key 。

用法:

const crypto.hkdf(digest, key, salt, info, keylen, callback)

參數:此函數采用以下參數作為參數。

  • digest:這是一串摘要算法。
  • key:這是秘密的鑰匙
  • salt:它代表著重要的價值。
  • info:它包含更多信息。
  • keylen:它是要生成的 key 的長度。
  • callback:這是用於進一步的操作。

返回值:此函數返回特定長度的派生 key 。

範例1:



index.js


// Node.js program to demonstrate the  
// crypto.hkdf() function
  
// Importing crypto module
const crypto = require('crypto')
  
// getting dervied key 
// by using hkdf() method
const val = crypto.hkdf('sha512', 'key', 'salt', 
                        'info', 64, (err, derivedKey) => {
  
    // checking if any error is found
    if (err) throw err;
  
    // display the result
    console.log(Buffer.from(derivedKey).toString('hex'));
  });

使用以下命令運行index.js文件。

node index.js

輸出:

24156e2c35525baaf3d0fbb92b734c8032a110a3f12e25
96e441e1924870d84c3a500652a723738024432451046fd237e
fad8392fb686c5277a59e0105391653

範例2:

index.js


// Node.js program to demonstrate the  
// crypto.hkdf() function
  
// Importing crypto module
const crypto = require('crypto')
  
// creating and intializing array buffer
const key = new ArrayBuffer(8);
  
// getting dervied key 
// by using hkdf() method
const val = crypto.hkdf('sha512', key, 'salt', 
                        'info', 32, (err, derivedKey) => {
  
    // checking if any error is found
    if (err) throw err;
  
    // display the result
    console.log(Buffer.from(derivedKey).toString('hex'));
  });

使用以下命令運行index.js文件。

node index.js

輸出:

bb105ac3235f285bd53b68cb95bf78c7fe5f3
a7924ac64d291f68ba2bd0430e1

參考:https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_hkdf_digest_key_salt_info_keylen_callback

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js scrypto.hkdf() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。