crypto.generateKeyPair() Method in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The crypto.generateKeyPair() can be used to generate a new asymmetric key pair of the specified type. Supported types for generating key pair are: RSA, DSA, EC, Ed25519, Ed448, X25519, X448 and DH. The function behaves as if keyObject.export has been called on its result when a publicKeyEncoding or privateKeyEncoding is specified, else the respective part of keyObject is returned.

Syntax

crypto.generateKeyPair(type, options, callback)

Parameters

The above parameters are described as below −

  • type – It holds the string type for which keys needs to be generated. Supported types are - RSA, DSA, EC, Ed25519, Ed448, X25519, X448 and DH.

  • options – It can hold the following Parameters −

    • modulusLength – This holds the key size in bits for type (RSA, DSA).

    • publicExponent – This holds the public exponent value for RSA algorithm.

      Default value is – 0x10001

    • divisorLength – This holds the size of q in bits.

    • namedCurve – This will hold the name of the curve to be used.

    • prime – This will hold the prime parameter for types like DH.

    • PrimeLength – This will hold the prime length in bits.

    • generator – This parameter holds the custom generator value, Default: 2.

    • groupName – This is the diffe-hellman group name for DH algorithm.

    • publicKeyEncoding – This will hold the string value for public key encoding.

    • privateKeyEncoding - This will hold the string value for private key encoding.

  • callback - The callback has the following Parameters −

    • err – This will hold the type of error.

    • publicKey – This will hold the value for public key.

    • PrivateKey – This will hold the value for private key.

Example

Create a file with name – generateKeyPair.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node generateKeyPair.js

generateKeyPair.js

// Node.js program to demonstrate the flow of crypto.generateKeyPair() method

// Importing generateKeyPair from crypto module
const { generateKeyPair } = require('crypto');

// Calling generateKeyPair() method with the below parameters
generateKeyPair('rsa', {
   modulusLength: 530, // options
   publicExponent: 0x10101,
   publicKeyEncoding: {
      type: 'pkcs1',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der',
      cipher: 'aes-192-cbc',
      passphrase: 'Welcome to TutorialsPoint!'
   }
}, (err, publicKey, privateKey) => { // Callback function
   if(!err)
   {
      // This will print the asymmetric key pair
      console.log("Public Key is: ", publicKey);
      console.log();
      console.log("Private Key is: ", privateKey);
   }
   else
   {
      // Prints error if any
      console.log("Errr is: ", err);
   }
});

Output

C:\home\node>> node generateKeyPair.js
Public Key is: <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 ... >

Private Key is: <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 ... >

Example

Let's take a look at one more example.

// Node.js program to demonstrate the flow of crypto.generateKeyPair() method

// Importing generateKeyPair from crypto module
const { generateKeyPair } = require('crypto');

// Calling generateKeyPair() method with the below parameters
generateKeyPair('ec', {
   namedCurve: 'secp256k1', // Options
   publicKeyEncoding: {
      type: 'spki',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
},(err, publicKey, privateKey) => { // Callback function
   if(!err)
   {
      // This will print the asymmetric key pair
      console.log("Public Key is: ", publicKey);
      console.log("Public Key in hex is: ", publicKey.toString('hex'));
      console.log();
      console.log("Private Key is: ", privateKey);
      console.log("Private Key in hex is: ",
      privateKey.toString('hex'));
   }else{
      // Prints error if any
      console.log("Errr is: ", err);
   }
});

Output

C:\home\node>> node generateKeyPair.js
Public Key is: <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 ... >
Public Key in hex is:
3056301006072a8648ce3d020106052b8104000a03420004d1d00b7ef7e33ecfd8082a20a85e52
beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afabaf794faf6f31bc8
c9f7007748f74767677c

Private Key is: <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 ... >
Private Key in hex is:
308184020100301006072a8648ce3d020106052b8104000a046d306b02010104200501a1d580f7
65566fa33a05d36dec82ad590ff8697d182ddca6b881acfbadd1a14403420004d1d00b7ef7e33e
cfd8082a20a85e52beca562942c36b9fd3157cf98b03b41ecc4b4e13b4cd5cd35814383ee76afa
baf794faf6f31bc8c9f7007748f74767677c
raja
Published on 20-May-2021 12:28:20
Advertisements