Node.js keyObject.asymmetricKeyDetails 属性

Node.js keyObject.asymmetricKeyDetails 属性

在本文中,我们将学习关于 KeyObject 类的 Node.js keyObject.asymmetricKeyDetails 属性 。KeyObject 类被用于表示特定算法的键,并包含用于处理加密键实例的内置函数或方法。 asymmetricKeyDetails 属性 将提供有关加密键对象的详细信息,其中包括您在方法中指定的基本详细信息或参数的值。

语法: 以下是 asymmetricKeyDetails 属性的语法。

key_object.asymmetricKeyDetails

以下示例将帮助理解此属性的用法。

示例1: 在此示例中,我们将使用RSASSA-PKCS1-v1_5算法生成加密密钥,并将此加密密钥转换为keyObject。该密钥将是私钥和公钥的对象,但我们只能将单个密钥转换为keyObject。首先,我们将从生成的对象中提取私钥和公钥,然后将两个密钥传递给 keyObject.from()方法 以生成keyObject。然后,我们将使用 key_object.asymmetricKeyDetails属性 获取生成的keyObject的详细信息。

// 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);
})();

输出:

private_key_object.asymmetricKeyDetails: { modulusLength: 4096, publicExponent: 197905n } 

public_key_object.asymmetricKeyDetails: { modulusLength: 4096, publicExponent: 197905n } 

示例2: 在这个示例中,我们将使用“RSA-PSS”算法生成加密密钥,并将其转换为keyObject,并获取生成密钥的详细信息。

//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);
})();

输出:

private_key_object.asymmetricKeyDetails:  { modulusLength: 2048, publicExponent: 197905n }
public_key_object.asymmetricKeyDetails:  { modulusLength: 2048, publicExponent: 197905n }

参考资料: https://nodejs.org/api/crypto.html#class-keyobject

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程