Node.js diffieHellman.getGenerator() Method
The diffieHellman.getGenerator() method is an inbuilt application programming interface of class diffieHellman within the crypto module which is used to get the generator value of DiffieHellman (dh) object.
Syntax:
diffieHellman.getGenerator([encoding])
Parameters: This method takes encoding as a parameter.
Return Value: It returns the DiffieHellman generator value in the specified encoding. If no encoding provided Buffer is returned else String is returned.
Example 1:
index.js
const { createDiffieHellman } = require( 'crypto' );
const dh = createDiffieHellman(512);
let dhGenerator = dh.getGenerator()
console.log('\nIs Buffer return ( encoding not specified ) : ' +
Buffer.isBuffer(dhGenerator)) // true
console.log(' Return value : ')
console.log(dhGenerator)
// Encoding specified
// Return String
dhGenerator = dh.getGenerator(' base64 ')
console.log(' \nIs Buffer return ( encoding specified ): ' +
Buffer.isBuffer(dhGenerator)) // true
console.log(' Return value :')
console.log(dhGenerator)
|
Run the index.js file using the following command:
node index.js
Output:
Is Buffer return ( encoding not specified ) : true
Return value :
<Buffer 02>
Is Buffer return ( encoding specified ): false
Return value :
Ag==
Example 2:
index.js
const { createDiffieHellman } = require( 'crypto' );
const alice = createDiffieHellman(512);
const alicePrime = alice.getPrime();
const aliceGenerator = alice.getGenerator()
const aliceKey = alice.generateKeys();
const bob = createDiffieHellman( alicePrime, aliceGenerator );
const bobPrime = bob.getPrime();
const bobGenerator = bob.getGenerator()
const bobKey = bob.generateKeys();
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
let isSymmetric = aliceSecret.toString( 'hex' ) == bobSecret.toString( 'hex' )
console.log( `Is Symmetric key generation
successful : ${ isSymmetric }` );
|
Run the index.js file using the following command:
node index.js
Output:
Is Symmetric key generation successful : true
Reference: https://nodejs.org/api/crypto.html#crypto_diffiehellman_getgenerator_encoding
Last Updated :
01 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...