Node.js keyObject.asymmetricKeyType 属性
keyObject.asymmetricKeyType 属性用于识别密钥的类型。密钥的类型实际上是用于构建密钥实例的算法名称。
Node.js的加密模块允许我们使用各种加密算法创建加密密钥实例和相应的密钥对象。
假设使用“RSA”算法生成非对称密钥(公钥和私钥),则该属性返回RSA。
语法: 下面是 keyObject.asymmetricKeyType 属性的语法。该属性适用于加密密钥对象。
keyObject_name.asymmetricKeyType;
为了演示使用此属性,我们将使用 keyObject() 方法创建一个加密密钥对象。
以下是了解密钥对象类型的必要步骤:
步骤1: 使用 subtle.generateKey() 方法生成加密密钥。该方法根据指定的算法生成对称或非对称密钥。
以下是生成加密密钥实例的语法:
const key = generateKey(
algorithm : Object,
extractable : boolean,
keyUsages : array
);
参数:
- Algorithm(算法): 这个对象指定要生成的密钥类型和其他算法细节。
- Extractable(可提取性): 一个布尔变量,指示是否可以使用特定技术(比如SubtleCrypto.exportKey())导出密钥。如果为true,则不能导出密钥。
- Keyusages(密钥用法): 一个列出使用生成的密钥的指示的数组。
返回: Crypto Key Instance(加密密钥实例)。
步骤2: 使用 KeyObject.from(key) 方法将此加密密钥实例转换为KeyObject(密钥对象)。此方法接受一个单个密钥实例(可以是公钥或私钥)从非对称密钥组中生成相应的KeyObject。以下是该方法的语法:
keyObject.form( key );
参数:
- key: 单个键实例(对称或非对称键)
返回值: KeyObject
步骤3: 现在我们已经创建了一个KeyObject,可以使用keyObject.asymmetricKeyType属性获取生成的KeyObject的类型。
keyObject.asymmetricKeyType属性的语法如下:
keyObject_name.asymmetricKeyType;
返回: 此属性返回一个字符串,告诉我们密钥的类型。
让我们通过示例来理解这个主题:
示例1: 在这个示例中,使用“RSA-PSS”算法生成加密密钥实例。此算法需要两个密钥 – 公钥和私钥。按照上述讨论的步骤 – 生成加密密钥实例,将加密密钥实例转换为KeyObject,并应用属性keyObject_name.asymmetricKeyType:
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: 4044,
// Unit8Array - consists of 8-bit unsigned integers.
publicExponent: new Unit8Array([1, 3, 1]),
// 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 privet_key_object = KeyObject.from(k.privateKey);
// printing the asymmetricKeyDetails of private KeyObject
console.log("privet_key_object.asymmetricKeyType: ",
privet_key_object.asymmetricKeyType);
})();
输出:
privet_key_object.asymmetricKeyType: rsa
密钥的类型是RSA,这意味着使用RSA密码算法生成密钥实例。
示例2: 在这个示例中,使用“ECDSA”算法生成加密密钥实例。
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: "ECDSA",
// Length of RSA modulus in bits (number of bits).
namedCurve: "P-384",
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 privet_key_object = KeyObject.from(k.privateKey);
// printing the asymmetricKeyDetails of private KeyObject
console.log("privet_key_object.asymmetricKeyType: ",
privet_key_object.asymmetricKeyType);
// Generating keyObject for public Key
const public_key_object = KeyObject.from(k.publicKey);
// printing the asymmetricKeyType of public KeyObject
console.log("public_key_object.asymmetricKeyType: ",
public_key_object.asymmetricKeyType);
})();
输出:
privet_key_object.asymmetricKeyType: ec
public_key_object.asymmetricKeyType: ec
密钥的类型是EC(椭圆曲线),这意味着使用椭圆曲线加密算法来生成密钥实例。
参考: https://nodejs.org/api/crypto.html#keyobjectasymmetrickeytype