Node.js KeyObject.from() Method
The keyObject.form() method is an inbuilt application programming interface of class keyObject within the crypto module which is used to convert the CryptoKey instance in KeyObject. It is a static method of the class KeyObject.
Syntax:
keyObject.form( key )
Parameters: This method takes only one parameter describing below.
- key: This parameter holds the CryptoKey value.
Return Value: This method returns the instance to KeyObject.
Example:
Filename: index.js
Javascript
// Node.js program to demonstrate the // keyObject.form() method // Importing the crypto module const { webcrypto: { subtle }, KeyObject } = require( 'crypto' ); // Generating the crypto 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 occurred" ); } })(); |
Run index.js file using below command:
node index.js
Output:
Successfully converted a cryptoKey to instance of keyObject.
Reference: https://nodejs.org/api/crypto.html#crypto_class_keyobject
Contact Us