当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js keyObject.asymmetricKeyDetails用法及代码示例


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

用法:以下是 asymmetryKeyDetails 属性的语法。

key_object.asymmetricKeyDetails

以下示例将有助于理解该属性的使用。

示例 1:在此示例中,我们将使用以下命令生成加密 key RSASSA-PKCS1-v1_5算法,并将将此加密 key 转换为 keyObject。 key 将是私钥和公钥的对象,但我们只能将单个 key 转换为 key 对象。首先,我们将从生成的对象中提取私钥和公钥,然后将两个 key 传递给keyObject.from()方法生成 keyObject。之后,我们将使用key_object.asymmetryKeyDetails 属性获取生成的 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);
})();

输出:

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

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

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

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

输出:

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

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



相关用法


注:本文由纯净天空筛选整理自harishcarpenter大神的英文原创作品 Node.js keyObject.asymmetricKeyDetails Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。