Skip to content
Related Articles

Related Articles

Improve Article
Node.js | crypto.generateKeyPair() Method
  • Last Updated : 27 Mar, 2020

The crypto.generateKeyPair() method is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type. For example, the currently supported key types are RSA, DSA, EC, Ed25519, Ed448, X25519, X448, and DH. Moreover, if option’s publicKeyEncoding or privateKeyEncoding is stated here, then this function acts as if keyObject.export() had been called on its output. Else, the particular part of the key is returned as a KeyObject.
However, it is suggested to encode the public keys as ‘spki’ and private keys as ‘pkcs8’ with encryption for long-term storage.

Syntax:

crypto.generateKeyPair( type, options, callback )

Parameters: This method accept three parameters as mentioned above and described below:

  • type: It holds a string and it must include one or more of the following algorithms: ‘rsa’, ‘dsa’, ‘ec’, ‘ed25519’, ‘ed448’, ‘x25519’, ‘x448’, or ‘dh’.
  • options: is of type object. It can hold the following parameters:
    1. modulusLength: It holds a number. It is the key size in bits and is applicable for RSA, and DSA algorithm only.
    2. publicExponent: It holds a number. It is the Public exponent of RSA algorithm. Its by default value is 0x10001.
    3. divisorLength: It holds a number. It is the size of q in bits of DSA algorithm.
    4. namedCurve: It holds a string. It is the name of the curve to be used in EC algorithm.
    5. prime: It holds a buffer. It is the prime parameter of DH algorithm.
    6. primeLength: It holds a number. It is the prime length of DH algorithm in bits.
    7. generator: It holds a number. It is the custom generator of DH algorithm. Its by default value is 2.
    8. groupName: It holds string. It is the Diffie-Hellman group name of DH algorithm.
    9. publicKeyEncoding: It holds a string.
    10. privateKeyEncoding: It holds an Object.
  • callback: It is a function, with parameters publicKey, privateKey and err.
    1. err: holds an error.
    2. publicKey: It holds a string, buffer or a KeyObject.
    3. privateKey: holds a string, buffer or a KeyObject.

Return Value: It returns a new asymmetric key pair of the given type.

Below examples illustrate the use of crypto.generateKeyPair() method in Node.js:



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

Output:

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 ... >

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

Output:

Public Key is:  3056301006072a8648ce3d020106052b8104000a0342000499c5f442c3264bcdfb093b0bc820e3f0f6546972856ebec2f8ccc03f49abdb47ffcfcaf4f37e0ec53050760e74014767e30a8a3e891f4db8c83fa27627898f15

Private Key is:  308184020100301006072a8648ce3d020106052b8104000a046d306b0201010420326b340a964512bfc3e010850ff05e077b2f016fce9eded11f40643e4231efc4a1440342000499c5f442c3264bcdfb093b0bc820e3f0f6546972856ebec2f8ccc03f49abdb47ffcfcaf4f37e0ec53050760e74014767e30a8a3e891f4db8c83fa27627898f15

Reference: https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback




My Personal Notes arrow_drop_up
Recommended Articles
Page :