在本文中,我們將了解 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
相關用法
- Node.js keyObject.asymmetricKeyType用法及代碼示例
- Node.js keyObject.symmetricKeySize用法及代碼示例
- Node.js keyObject.equals()用法及代碼示例
- Node.js urlObject.auth()用法及代碼示例
- Node.js process.env()用法及代碼示例
- Node.js process.argv0()用法及代碼示例
- Node.js process.argv()用法及代碼示例
- Node.js process.arch()用法及代碼示例
- Node.js Decipher.final()用法及代碼示例
- Node.js crypto.createDiffieHellman()用法及代碼示例
- Node.js v8.deserializer.readHeader()用法及代碼示例
- Node.js v8.deserializer.readRawBytes()用法及代碼示例
- Node.js v8.deserializer.readUint32()用法及代碼示例
- Node.js v8.deserializer.readUint64()用法及代碼示例
- Node.js v8.deserializer.readValue()用法及代碼示例
- Node.js v8.serializer.releaseBuffer()用法及代碼示例
- Node.js v8.serializer.writeDouble()用法及代碼示例
- Node.js v8.serializer.writeHeader()用法及代碼示例
- Node.js v8.serializer.writeRawBytes()用法及代碼示例
- Node.js v8.serializer.writeUint32()用法及代碼示例
- Node.js v8.serializer.writeUint64()用法及代碼示例
- Node.js v8.serializer.writeValue()用法及代碼示例
- Node.js Buffer.allocUnsafe()用法及代碼示例
- Node.js Buffer.allocUnsafeSlow()用法及代碼示例
- Node.js Buffer.byteLength()用法及代碼示例
注:本文由純淨天空篩選整理自harishcarpenter大神的英文原創作品 Node.js keyObject.asymmetricKeyDetails Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。