Open In App

Node.js keyObject.asymmetricKeyDetails Property

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Importing the crypto module
const { Console } = require('node:console');
const {
    webcrypto: { subtle }, KeyObject
} = require('node:crypto');
 
// Async function
(async function () {
 
    // Generating the crypto key
    // RSA Algorithm
    const k = await subtle.generateKey(
        {
            // Algorithm name
            name: "RSASSA-PKCS1-v1_5",
            // Length of RSA modulus in bits (number of bits)   
            modulusLength: 4096,
            // Unit8Array -  consists of 8-bit unsigned integers
            publicExponent: new Unit8Array([3, 5, 17]),
            // digital hash function
            hash: "SHA-256"
 
        }
        // Key is not exportable.
        , false,
        // Key can be used for generating and verifying
        // the digital signature.
        ['sign', 'verify']
    );
 
    // Generating keyObject for private Key
    const private_key_object = KeyObject.from(k.privateKey);
 
    // Printing the asymmetricKeyDetails of private KeyObject
    console.log("private_key_object.asymmetricKeyDetails: ",
        private_key_object.asymmetricKeyDetails);
 
    // Generating keyObject for public Key
    const public_key_object = KeyObject.from(k.publicKey);
 
    // Printing the asymmetricKeyDetails  of public KeyObject
    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




//Importing the crypto module
const { Console } = require('node:console');
const {
    webcrypto: { subtle }, KeyObject
} = require('node:crypto');
 
// Async function
(async function () {
 
    // Generating the crypto key
    // RSA  Algorithm
    const k = await subtle.generateKey(
        {
            // Algorithm name
            name: "RSA-PSS",
            // Length of RSA modulus in bits (number of bits)
            modulusLength: 2048,
            // Unit8Array -  consists of 8-bit unsigned integers
            publicExponent: new Unit8Array([3, 5, 17]),
            // Digital hash function
            hash: "SHA-256"
        }
        // Key is not exportable
        , false,
        // Key can be used for generating and verifying
        // the digital signature
        ['sign', 'verify']
    );
 
    // Generating keyObject for private Key
    const private_key_object = KeyObject.from(k.privateKey);
 
    // printing the asymmetricKeyDetails of private KeyObject
    console.log("private_key_object.asymmetricKeyDetails: ",
        private_key_object.asymmetricKeyDetails);
 
    // Generating keyObject for public Key
    const public_key_object = KeyObject.from(k.publicKey);
 
    // printing the asymmetricKeyDetails  of public KeyObject
    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



Last Updated : 22 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads