Node.js 中的 crypto.generateKeyPair() 方法

所述可用于产生新的非对称密钥对指定类型的。支持生成密钥对的类型有:RSA、DSA、EC、Ed25519、Ed448、X25519、X448 和 DH。当指定了 publicKeyEncoding 或 privateKeyEncoding 时,该函数的行为就像在其结果上被调用一样,否则返回 keyObject 的相应部分。crypto.generateKeyPair()keyObject.export

语法

crypto.generateKeyPair(type, options, callback)

参数

上述参数描述如下 -

  • type  - 它保存需要为其生成密钥的字符串类型。支持的类型有 - RSA、DSA、EC、Ed25519、Ed448、X25519、X448 和 DH。

  • 选项 - 它可以保存以下参数 -

    • modulusLength - 这保存了类型(RSA、DSA)的密钥大小(以位为单位)。

    • publicExponent  – 保存 RSA 算法的公共指数值。

      默认值为 – 0x10001

    • divisorLength  – 这以位为单位保存 q 的大小。

    • namedCurve – 这将保存要使用的曲线的名称。

    • prime – 这将保存 DH 等类型的素数参数。

    • PrimeLength  – 这将保存以位为单位的素数长度。

    • generator – 此参数保存自定义生成器值,默认值:2。

    • groupName  – 这是 DH 算法的差异赫尔曼组名称。

    • publicKeyEncoding  – 这将保存公钥编码的字符串值。

    • privateKeyEncoding  - 这将保存私钥编码的字符串值。

  • 回调 - 回调具有以下参数 -

    • err  – 这将保存错误的类型。

    • publicKey  – 这将保存公钥的值。

    • PrivateKey – 这将保存私钥的值。

示例

创建一个具有名称的文件 -generateKeyPair.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -

node generateKeyPair.js

生成密钥对.js

//Node.jsprogram 演示了 crypto.generateKeyPair() 方法的流程

// 从加密模块导入 generateKeyPair
const { generateKeyPair } = require('crypto');

// 使用以下参数调用 generateKeyPair() 方法
generateKeyPair('rsa', {
   modulusLength: 530, // 选项
   publicExponent: 0x10101,
   publicKeyEncoding: {
      type: 'pkcs1',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der',
      cipher: 'aes-192-cbc',
      passphrase: 'Welcome to nhooo!'
   }
}, (err, publicKey, privateKey) => { // 回调函数
   if(!err)
   {
      // 这将打印非对称密钥对
      console.log("公钥是: ", publicKey);
      console.log();
      console.log("私钥是: ", privateKey);
   }
   else
   {
      // 打印错误(如果有)
      console.log("错误是: ", err);
   }
});

输出结果

C:\home\node>> node generateKeyPair.js
公钥是: <Buffer 30 4a 02 43 03 43 66 5a e1 25 0d d8 c4 fe e3 a0 77 ea
e4 e7 20 78 8c eb a7 a4 f6 85 f0 45 fc 7b 46 d7 3e 5e e3 8b a1 16 e5 59 e8 79
69 65 54 00 6c 89 ... >

私钥是: <Buffer 30 82 01 cd 30 57 06 09 2a 86 48 86 f7 0d 01 05 0d 30
4a 30 29 06 09 2a 86 48 86 f7 0d 01 05 0c 30 1c 04 08 1e a2 b4 ea ce 50 0e 80
02 02 08 00 30 0c ... >

示例

让我们再看一个例子。

//Node.jsprogram 演示了 crypto.generateKeyPair() 方法的流程

// 从加密模块导入 generateKeyPair
const { generateKeyPair } = require('crypto');

// 使用以下参数调用 generateKeyPair() 方法
generateKeyPair('ec', {
   namedCurve: 'secp256k1', // 选项
   publicKeyEncoding: {
      type: 'spki',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
},(err, publicKey, privateKey) => { // 回调函数
   if(!err)
   {
      // 这将打印非对称密钥对
      console.log("公钥是: ", publicKey);
      console.log("十六进制公钥是: ", publicKey.toString('hex'));
      console.log();
      console.log("私钥是: ", privateKey);
      console.log("十六进制的私钥是: ",
      privateKey.toString('hex'));
   }else{
      // 打印错误(如果有)
      console.log("错误是: ", err);
   }
});

输出结果

C:\home\node>> node generateKeyPair.js
公钥是: <Buffer 30 56 30 10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04
00 0a 03 42 00 04 d1 d0 0b 7e f7 e3 3e cf d8 08 2a 20 a8 5e 52 be ca 56 29 42
c3 6b 9f d3 15 7c ... >
十六进制公钥是:
3056301006072a8648ce3d020106052b8104000a03420004d1d00b7ef7e33ecfd8082a20a85e52
beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afabaf794faf6f31bc8
c9f7007748f74767677c

私钥是: <Buffer 30 81 84 02 01 00 30 10 06 07 2a 86 48 ce 3d 02 01 06
05 2b 81 04 00 0a 04 6d 30 6b 02 01 01 04 20 05 01 a1 d5 80 f7 65 56 6f a3 3a
05 d3 6d ec 82 ad ... >
十六进制的私钥是:
308184020100301006072a8648ce3d020106052b8104000a046d306b02010104200501a1d580f7
65566fa33a05d36dec82ad590ff8697d182ddca6b881acfbadd1a14403420004d1d00b7ef7e33e
cfd8082a20a85e52beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afa
baf794faf6f31bc8c9f7007748f74767677c