Node.js diffieHellman.computeSecret() Method
The diffieHellman.computeSecret() method is an inbuilt application programming interface of class DiffieHellman within the crypto module which is used to compute the DiffieHellman (dh) secret key.
Syntax:
diffieHellman.computeSecret(otherPublicKey[,
inputEncoding][, outputEncoding])
Parameters: This method accepts the following parameters.
- otherPublicKey: This method takes the other party’s public key as the first parameter.
- inputEncoding: Specify the encoding of otherPublicKey. If it is not provided otherPublicKey expected to be Buffer, TypedArray, or DataView.
- outputEncoding: If it is provided String is returned otherwise Buffer is returned.
Return Value: This method returns the diffieHellman shared secret key.
Example 1:
index.js
const crypto = require( 'crypto' )
const alice = crypto.createDiffieHellman(512);
const bob = crypto.createDiffieHellman(
alice.getPrime(), alice.getGenerator());
const aliceKey = alice.generateKeys( 'base64' );
const bobKey = bob.generateKeys( 'base64' );
const aliceSecret = alice.computeSecret(bobKey, 'base64' , 'hex' );
const bobSecret = bob.computeSecret(aliceKey, 'base64' , 'hex' );
if ( aliceSecret === bobSecret )
console.log(`Symmetric key : ${ aliceSecret }`)
|
Run the index.js file using the following command:
node index.js
Output:
Symmetric key : da884ccb0e24bf7e748f66998550b
f21f96b887e1f936478cdbc63b7806bd2403fd3aa28e5dbf58
bbabeb6f829dd86453eb0985b5ff593fcf7a8e1da20256b2a
Example 2: Take another example without providing outputEncoding.
index.js
const crypto = require( 'crypto' )
const alice = crypto.createDiffieHellman( 'modp15' );
const bob = crypto.createDiffieHellman( 'modp15' );
const aliceKey = alice.generateKeys( 'base64' );
const bobKey = bob.generateKeys( 'hex' );
const aliceSecret = alice.computeSecret(bobKey, 'hex ');
// Pass inputEncoding = ' base64 ' because Alice' s
const bobSecret = bob.computeSecret(aliceKey, 'base64' );
const isSymmetric = aliceSecret.equals(bobSecret)
if ( isSymmetric )
console.log( 'Symmetric key : ' , aliceSecret)
|
Run the index.js file using the following command:
node index.js
Output:
Symmetric key : <Buffer 6d 1a 6c 00 34 6c>
Example 3: Another example without providing inputEncoding. Pass inputEncoding as null.
Filename: index.js
Javascript
const crypto = require( 'crypto' )
const alice = crypto.createDiffieHellman( 'modp15' );
const bob = crypto.createDiffieHellman( 'modp15' );
alice.generateKeys();
bob.generateKeys();
const aliceSecret = alice.computeSecret(
bob.getPublicKey() , null , 'base64' );
const bobSecret = bob.computeSecret(
alice.getPublicKey(), null , 'base64' );
if (aliceSecret === bobSecret)
console.log(`Symmetric key : ${ aliceSecret }`)
|
Run the index.js file using the following command:
node index.js
Output:
Symmetric key : abLoALX9
Reference:
https://nodejs.org/api/crypto.html#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding
Last Updated :
10 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...