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
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自aditya32123大神的英文原創作品 Node.js KeyObject.from() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。