Node.js - diffieHellman.computeSecret() Method


The diffieHellman.computeSecret() is used for computing the shared secret using the public key of other's party and returning the computed shared secret. The suppliedKey is interpreted using the specified inputEncoding and the secret is encoded using the specified outputEncoding. If the inputEncoding is not specified the other publicKey is expected to be a buffer, DataView.

Syntax

diffieHellman.computeSecret(otherPublicKey, [inputEncoding], [outputEncoding])

Parameters

  • otherPublicKey – This is the public key that is used to compute the secret.

  • inputEncoding – This encoding is used to interpret the supplied key.

  • outputEncoding – This encoding is used for encoding the computed secret value.

Example 1

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

// diffieHellman.computeSecret() Demo Example

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

// Creating diffieHellman instances
const a = crypto.createDiffieHellman('modp15');
const b = crypto.createDiffieHellman('modp15');

// Creating keys using base64
const keyA = a.generateKeys('base64');

// Creating key using hex
const keyB = b.generateKeys('hex');

// Creating secret using diffieHellman
const secretA = a.computeSecret(keyB, 'hex');

// Creating secret using diffieHellman
const secretB = b.computeSecret(keyA, 'base64');

// Checking if the resultant secrets are equal
const isSymmetric = secretA.equals(secretB)

if ( isSymmetric )
   console.log('Symmetric key A: ', secretA)
   console.log('Symmetric key B: ', secretB)

Output

C:\home
ode>> node computeSecret.js Symmetric key A: <Buffer 43 4d aa ec dc c6> Symmetric key B: <Buffer 43 4d aa ec dc c6>

Example 2

Let's take a look at one more example −

// diffieHellman.computeSecret() Demo Example

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

// Creating diffieHellman instances
const a = crypto.createDiffieHellman(512);
const b = crypto.createDiffieHellman(a.getPrime(), a.getGenerator());

// Creating keys using base64
const keyA = a.generateKeys('base64');
const keyB = b.generateKeys('base64');

// Creating secret using diffieHellman
const secretA = a.computeSecret(keyB, 'base64', 'hex');
const secretB = b.computeSecret(keyA, 'base64', 'hex');

// Checking if the resultant secrets are equal
if( secretA === secretB )
   console.log(`Symmetric key A: ${ secretA }`)
   console.log(`Symmetric key B: ${ secretB }`)

Output

C:\home
ode>> node computeSecret.js Symmetric key A: 44badc3e4ad43d7c887190104a63f0b68222678da1cae92b54a146c243f7b354061e958e59aebce43f72016123bd133d66135e3341ce47a18a1d7a07c86c8ddb Symmetric key B: 44badc3e4ad43d7c887190104a63f0b68222678da1cae92b54a146c243f7b354061e958e59aebce43f72016123bd133d66135e3341ce47a18a1d7a07c86c8ddb

Updated on: 17-Jan-2022

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements