Node.js KeyObject.from() Method
Last Updated :
10 Jun, 2022
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
const { webcrypto: { subtle }, KeyObject }
= require( 'crypto' );
(async function () {
const key = await subtle.generateKey({
name: 'HMAC' ,
hash: 'SHA-256' ,
length: 256
}, true , [ 'sign' , 'verify' ]);
try {
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
Share your thoughts in the comments
Please Login to comment...