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


Node.js KeyObject.from()用法及代碼示例

keyObject.form()方法是加密模塊中keyObject類的內置應用程序編程接口,該接口用於轉換KeyObject中的CryptoKey實例。它是KeyObject類的靜態方法。

用法:

keyObject.form( key ) 

參數:該方法僅采用下麵描述的一個參數。

  • key:此參數保存CryptoKey值。

返回值:此方法將實例返回給KeyObject。



例:

index.js


// Node.js program to demonstrate the 
// keyObject.form() method 
  
// Importing the crypto module
const { webcrypto:{ subtle }, KeyObject } 
    = require('crypto');
  
// Genrating the cypto key that is 
// not a keyObject instance
(async function () {
    const key = await subtle.generateKey({
        name:'HMAC',
        hash:'SHA-256',
        length:256
    }, true, ['sign', 'verify']);
  
    try {
  
        // Calling keyObject.form() method to
        // get the instance to keyObject
        const keyObject = KeyObject.from(key);
        console.log("Successfully converted a "
        + "cryptoKey to instance of keyObject");
    }
    catch (error) {
        console.log("Error has been occured");
    }
})();

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

node index.js

輸出:

Successfully converted a cryptoKey to instance of keyObject.

參考:https://nodejs.org/api/crypto.html#crypto_class_keyobject

相關用法


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