Node.js keyObject.asymmetricKeyDetails Property
Last Updated :
22 Feb, 2023
In this article, we will learn about NodeJS keyObject.asymmetricKeyDetails property of the KeyObject class. The KeyObject class is used to represent algorithm-specific keys and contains built-in functions or methods for working with cryptographic key instances. The asymmetricKeyDetails property will give you details about the cryptographic key object that include basic details or the values ​​of the parameters you specified in the method.
Syntax: The following is the syntax for the asymmetricKeyDetails property.
key_object.asymmetricKeyDetails
The following examples will help to understand the use of this property.
Example 1: In this example, we will generate crypto keys using the RSASSA-PKCS1-v1_5 algorithm, and will convert this encryption key to a keyObject. The key will be an object of private and public keys but we can only convert the single key into a key object. First, we will extract the private and public keys from the generated object and then we will pass both keys to keyObject.from() method to generate the keyObject. After that, we will use the key_object.asymmetricKeyDetails property to get the details of the generated keyObject.
Javascript
const { Console } = require( 'node:console' );
const {
webcrypto: { subtle }, KeyObject
} = require( 'node:crypto' );
(async function () {
const k = await subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5" ,
modulusLength: 4096,
publicExponent: new Unit8Array([3, 5, 17]),
hash: "SHA-256"
}
, false ,
[ 'sign' , 'verify' ]
);
const private_key_object = KeyObject.from(k.privateKey);
console.log( "private_key_object.asymmetricKeyDetails: " ,
private_key_object.asymmetricKeyDetails);
const public_key_object = KeyObject.from(k.publicKey);
console.log( "public_key_object.asymmetricKeyDetails: " ,
public_key_object.asymmetricKeyDetails);
})();
|
Output:
private_key_object.asymmetricKeyDetails: { modulusLength: 4096, publicExponent: 197905n }
public_key_object.asymmetricKeyDetails: { modulusLength: 4096, publicExponent: 197905n }
Example 2: In this example, we will generate crypto keys using the “RSA-PSS” algorithm, and will convert this encryption key to a keyObject, and will get the details for the generated keys.
Javascript
const { Console } = require( 'node:console' );
const {
webcrypto: { subtle }, KeyObject
} = require( 'node:crypto' );
(async function () {
const k = await subtle.generateKey(
{
name: "RSA-PSS" ,
modulusLength: 2048,
publicExponent: new Unit8Array([3, 5, 17]),
hash: "SHA-256"
}
, false ,
[ 'sign' , 'verify' ]
);
const private_key_object = KeyObject.from(k.privateKey);
console.log( "private_key_object.asymmetricKeyDetails: " ,
private_key_object.asymmetricKeyDetails);
const public_key_object = KeyObject.from(k.publicKey);
console.log( "public_key_object.asymmetricKeyDetails: " ,
public_key_object.asymmetricKeyDetails);
})();
|
Output:
private_key_object.asymmetricKeyDetails: { modulusLength: 2048, publicExponent: 197905n }
public_key_object.asymmetricKeyDetails: { modulusLength: 2048, publicExponent: 197905n }
Reference: https://nodejs.org/api/crypto.html#class-keyobject
Share your thoughts in the comments
Please Login to comment...