Node.js - diffieHellman.setPublicKey() Method


The diffieHellman.setPublicKey() sets the Diffie-Hellman generated public key. The private key is a string if the encoding argument is provided. If no encoding is provided, the privateKey will be of type buffer, TypedArray or DataView.

Syntax

diffieHellman.setPublicKey( publicKey, [encoding] )

where, encoding is the parameter that specifies the encoding of the public Key.

Example 1

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

// diffieHellman.setPublicKey() Demo Example

// Importing the crypto module
const crypto = require('crypto')

// Generating the key pairs(public & private)
crypto.generateKeyPair('rsa',
   {
      modulusLength: 530,
      primeLength: 512,
      publicKeyEncoding: {
         type: 'spki',
         format: 'der'
      },
   privateKeyEncoding: {
      type: 'pkcs8',
      format: 'der'
   }
},
createDiffieHellman
)

function createDiffieHellman(err, publicKey, publicKey){
   // Initializing diffieHellman
   const dh = crypto.createDiffieHellman(512)
   // Setting the publicKey key
   dh.setPublicKey(publicKey)

   if( publicKey.equals(dh.getPublicKey()) )
      console.log(publicKey)
      console.log("DH publicKey is set successfully")
}

Output

C:\home
ode>> node publicKey.js <Buffer 30 82 01 5f 02 01 00 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 04 82 01 49 30 82 01 45 02 01 00 02 43 02 e0 2c ab 4d f9 8a ab 2e 37 92 df 01 a5 b4 ... > DH publicKey is set successfully

Example 2

Let's take a look at one more example −

// diffieHellman.setPublicKey() Demo Example

// Importing the crypto module
const crypto = require('crypto')

// Generating the key pairs(public & private)
crypto.generateKeyPair('dsa',
   {
      modulusLength: 530,
      primeLength: 512,
      publicKeyEncoding: {
         type: 'spki',
         format: 'der'
      },
      privateKeyEncoding: {
         type: 'pkcs8',
         format: 'der'
      }
   },
   createDiffieHellman
)

function createDiffieHellman(err, publicKey, publicKey){
   // Encoding the key in base64
   publicKey = publicKey.toString('base64');
   // Initializing diffieHellman
   const dh = crypto.createDiffieHellman( 512 )
   // Setting the diffieHellman's privateKey
   dh.setPublicKey( publicKey, 'base64' )

   // Checking equality between both keys
   if( publicKey === dh.getPublicKey('base64') )
      console.log(publicKey)
      console.log( "Successfully assigned the public key" )
}

Output

C:\home
ode>> node publicKey.js MIHnAgEAMIHABgcqhkjOOAQBMIG0AkkAnTspVsa5ck4xDnt4/xVGdIPXfOSTyePfGDliGbZLLErNQWVfykNc7fIMetCo6AeKVJyGNT+d3U6fFp+/HRXxXvqdITB39NcRAh0A72mnUzdgBxgjV+eYTYSVGuSjdClMli0CEiLyjQJIHZpvRyoeYvrb4A74pkMSoV51KBF4zi/667ypv3UZ0H9w0sXuy5SXuW1/0ngQWkrTECZayH/HahOWwwxQGCSX6a7bcAtgQ4QVBB8CHQC4Vqyod315EH6fsJykRzF+y6o3oAQc5Pf7Pu6l Successfully assigned the public key

Updated on: 17-Jan-2022

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements