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


Node.js crypto.generateKeyPair()用法及代码示例


crypto.generateKeyPair()方法是加密模块的内置应用程序编程接口,用于生成指定类型的新非对称 key 对。例如,当前支持的 key 类型是RSA,DSA,EC,Ed25519,Ed448,X25519,X448和DH。此外,如果在此说明了选项的publicKeyEncoding或privateKeyEncoding,则此函数的作用就好像在其输出上调用了keyObject.export()一样。否则,键的特定部分将作为KeyObject返回。但是,建议使用long-term存储的加密方式将公共 key 编码为“ spki”,将私有 key 编码为“ pkcs8”。

用法:

crypto.generateKeyPair( type, options, callback )

参数:此方法接受上述和以下所述的三个参数:



  • type:它包含一个字符串,并且必须包含以下一种或多种算法:“ rsa”,“ dsa”,“ ec”,“ ed25519”,“ ed448”,“ x25519”,“ x448”或“ dh”。
  • options:是对象类型。它可以包含以下参数:
    1. modulusLength:它包含一个数字。它是以位为单位的 key 大小,仅适用于RSA和DSA算法。
    2. publicExponent:它包含一个数字。它是RSA算法的公有 index 。默认情况下,其值为0x10001。
    3. divisorLength:它包含一个数字。它是DSA算法中q的大小(以位为单位)。
    4. namedCurve:它包含一个字符串。它是EC算法中要使用的曲线的名称。
    5. prime:它拥有一个缓冲区。它是DH算法的主要参数。
    6. primeLength:它包含一个数字。它是DH算法的素数长度(以位为单位)。
    7. generator:它包含一个数字。它是DH算法的自定义生成器。其默认值为2。
    8. groupName:它保存字符串。这是DH算法的Diffie-Hellman组名。
    9. publicKeyEncoding:它包含一个字符串。
    10. privateKeyEncoding:它拥有一个对象。
  • callback:它是一个带有参数publicKey,privateKey和err的函数。
    1. err:持有一个错误。
    2. publicKey:它包含一个字符串,缓冲区或KeyObject。
    3. privateKey:保存字符串,缓冲区或KeyObject。

返回值:它返回给定类型的新非对称 key 对。

以下示例说明了Node.js中crypto.generateKeyPair()方法的使用:

范例1:

// Node.js program to demonstrate the 
// crypto.generateKeyPair() method 
  
// Including generateKeyPair from crypto module 
const { generateKeyPair } = require('crypto'); 
  
// Calling generateKeyPair() method 
// with its parameters 
generateKeyPair('rsa', { 
  modulusLength:530,    // options 
  publicExponent:0x10101, 
  publicKeyEncoding:{ 
    type:'pkcs1', 
    format:'der'
  }, 
  privateKeyEncoding:{ 
    type:'pkcs8', 
    format:'der', 
    cipher:'aes-192-cbc', 
    passphrase:'GeeksforGeeks is a CS-Portal!'
  } 
}, (err, publicKey, privateKey) => { // Callback function 
       if(!err) 
       { 
         // Prints new asymmetric key pair 
         console.log("Public Key is:", publicKey); 
         console.log(); 
         console.log("Private Key is:", privateKey); 
       } 
       else
       { 
         // Prints error 
         console.log("Errr is:", err); 
       } 
         
  });

输出:

Public Key is:<Buffer 30 4a 02 43 03 12 b9
4c 1a 3f 96 07 51 c6 31 02d7 11 e2 e3 a5 2b 0c
7c 18 55 88 39 04 4c 86 e2 77 c4 29 47 82 2c 5b
4b 9e f3 e8 83 4b 5d 4b 31 e7 d5 ... >

Private Key is:<Buffer 30 82 01 cd 30 57 06
09 2a 86 48 86 f7 0d 01 050d 30 4a 30 29 06 09
2a 86 48 86 f7 0d 01 05 0c 30 1c 04 08 e0 31 2b
a0 38 82 e1 db 02 02 08 00 30 0c ... >

范例2:

// Node.js program to demonstrate the 
// crypto.generateKeyPair() method 
  
// Including generateKeyPair from crypto module 
const { generateKeyPair } = require('crypto'); 
  
// Calling generateKeyPair() method 
// with its parameters 
generateKeyPair('ec', { 
  namedCurve:'secp256k1',   // Options 
  publicKeyEncoding:{ 
    type:'spki', 
    format:'der'
  }, 
  privateKeyEncoding:{ 
    type:'pkcs8', 
    format:'der'
  } 
}, 
 (err, publicKey, privateKey) => { // Callback function 
       if(!err) 
       { 
         // Prints new asymmetric key 
         // pair after encoding 
         console.log("Public Key is:", 
                  publicKey.toString('hex')); 
         console.log(); 
         console.log("Private Key is:", 
                 privateKey.toString('hex')); 
       } 
       else
       { 
         // Prints error 
         console.log("Errr is:", err); 
       } 
         
  });

输出:

Public Key is: 3056301006072a8648ce3d020106052b8104000a0342000499c5f442c3264bcdfb093b0bc820e3f0f6546972856ebec2f8ccc03f49abdb47ffcfcaf4f37e0ec53050760e74014767e30a8a3e891f4db8c83fa27627898f15

Private Key is: 308184020100301006072a8648ce3d020106052b8104000a046d306b0201010420326b340a964512bfc3e010850ff05e077b2f016fce9eded11f40643e4231efc4a1440342000499c5f442c3264bcdfb093b0bc820e3f0f6546972856ebec2f8ccc03f49abdb47ffcfcaf4f37e0ec53050760e74014767e30a8a3e891f4db8c83fa27627898f15

参考: https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback




相关用法


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