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:
// Node.js program to demonstrate the // diffieHellman.computeSecret() method const crypto = require( 'crypto' )
// Instance of diffieHellman class... const alice = crypto.createDiffieHellman(512); const bob = crypto.createDiffieHellman( alice.getPrime(), alice.getGenerator());
// Generate otherPublicKey // Pass 'base64' as encoding parameter const aliceKey = alice.generateKeys( 'base64' );
const bobKey = bob.generateKeys( 'base64' );
// Exchange and generate the secret... // inputEncoding = 'base64' because otherPublicKey // is 'base64' encoded // Return value should be 'hex' encoded because // outputEncoding = 'hex' 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.
// Node.js program to demonstrate the // diffieHellman.computeSecret() method const crypto = require( 'crypto' )
// Instances of diffieHellman class const alice = crypto.createDiffieHellman( 'modp15' );
const bob = crypto.createDiffieHellman( 'modp15' );
// Pass 'base64' as encoding parameter const aliceKey = alice.generateKeys( 'base64' );
// Pass 'hex' as encoding parameter const bobKey = bob.generateKeys( 'hex' );
// Pass inputEncoding = 'hex' because Bob's key // 'hex' encocded // No outputEncoding provided // Return Buffer const aliceSecret = alice.computeSecret(bobKey, 'hex ');
// Pass inputEncoding = ' base64 ' because Alice' s
// key 'base64' encocded // No outputEncoding provided // Return Buffer 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
// Node.js program to demonstrate the // diffieHellman.computeSecret() method const crypto = require( 'crypto' )
// Instances of diffieHellman class const alice = crypto.createDiffieHellman( 'modp15' );
const bob = crypto.createDiffieHellman( 'modp15' );
// Generate Key alice.generateKeys(); bob.generateKeys(); // Compute Secret // inputEncoding is null // otherPublicKey expected to be Buffer 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: