crypto.generateKeyPairSync() Method in Node.js


The crypto.generateKeyPairSync() can be used to generate a new asymmetric key pair of the specified type in a sync flow. 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. The suggested type for public key is 'spki' and for private key it is 'pkcs8'.

Syntax

crypto.generateKeyPairSync(type, options)

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.

Example

Create a file with name – generateKeyPairSync.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 generateKeyPairSync.js

generateKeyPairSync.js

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

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

//Getting the value of publicKye and privateKey in a sync process
const { publicKey, privateKey } = generateKeyPairSync('ec', {
   namedCurve: 'secp256k1', // Implementing options
   publicKeyEncoding: {
      type: 'spki',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
});

// Printing the asymmetric key pair in a sync process
console.log("The public key is: ", publicKey);
console.log();
console.log("The private key is: ", privateKey);

Output

C:\home
ode>> node generateKeyPairSync.js The 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 a1 76 dd f0 fe 96 cc 28 59 a5 45 16 58 86 ca 3b 56 1e 04 ee b0 de 28 67 0a 70 ... > The 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 e6 f0 69 2e b0 35 7d 0b 5c ba 76 fc dc 9f 95 ae d7 ... >

Example

Let's take a look at one more example.

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

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

//Getting the value of publicKye and privateKey in a sync process
const { publicKey, privateKey } = generateKeyPairSync('dsa', {
   modulusLength: 570, //Implementing options
   publicKeyEncoding: {
      type: 'spki',
      format: 'der'
   },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
});

// Printing asymmetric key pair after encoding
console.log("Public Key is: ", publicKey)
console.log("The public key value in base64 is: ",
   publicKey.toString('base64'));
console.log("------------------------------------------------------")
console.log("Private Key is: ", privateKey)
console.log("The private key in base64 is: ",
   privateKey.toString('base64'));

Output

C:\home
ode>> node generateKeyPairSync.js Public Key is: <Buffer 30 82 01 0f 30 81 bf 06 07 2a 86 48 ce 38 04 01 30 81 b3 02 49 00 9a 5c dd a3 ce 0e 8e 3e 0e ed 11 96 13 fe 1c a6 f6 35 27 0c 60 f9 51 ee dd 2c 75 12 ... > The public key value in base64 is: MIIBDzCBvwYHKoZIzjgEATCBswJJAJpc3aPODo4+Du0RlhP+HKb2NScMYPlR7t0sdRJhr0JWPvtRyF Wmn5ZAldFdDrUye5eQ+HmwgJboEWtCUm3b24CoLSQ74P1YkwIdAJs5rCSAIefaTT469xx+/8C3jS4W jYpHci0rft8CR3Fx1wxDFdCHJBqPlR7iGxd+7nZlChABL7UqCZMaiwCJ2ijVXc5dgr3Frudu7CbaAn RJStbqDjm5ppj4aaZV/9FmKvWVao9wA0sAAkhQtXOIWQrHde+fXoZLgPhbTBctPB1tcFztNmq2s3IO KGfo2kFUL6eJu811SSZ1scQFLVKc5DrZIdW7t3UqzEH+xCVxNkWtGQk= ------------------------------------------------------ Private Key is: <Buffer 30 81 e5 02 01 00 30 81 bf 06 07 2a 86 48 ce 38 04 01 30 81 b3 02 49 00 9a 5c dd a3 ce 0e 8e 3e 0e ed 11 96 13 fe 1c a6 f6 35 27 0c 60 f9 51 ee dd 2c ... > The private key in base64 is: MIHlAgEAMIG/BgcqhkjOOAQBMIGzAkkAmlzdo84Ojj4O7RGWE/4cpvY1Jwxg+VHu3Sx1EmGvQlY++1 HIVaaflkCV0V0OtTJ7l5D4ebCAlugRa0JSbdvbgKgtJDvg/ViTAh0AmzmsJIAh59pNPjr3HH7/wLeN LhaNikdyLSt+3wJHcXHXDEMV0IckGo+VHuIbF37udmUKEAEvtSoJkxqLAInaKNVdzl2CvcWu527sJt oCdElK1uoOObmmmPhpplX/0WYq9ZVqj3AEHgIcJ2ON17GGE4FrtkJak337GB+bAEkb+YjulN2rug==

Updated on: 20-May-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements