Node.js diffieHellman.getGenerator() Method Last Updated : 01 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // Node.js program to demonstrate the // diffieHellman.getGenerator() method // Destructure createDiffieHellman method from crypto const { createDiffieHellman } = require('crypto'); // Instances of the DiffieHellman class const dh = createDiffieHellman(512); // Generate dh's Generator // No encoding specified // Return Buffer 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 // Node.js program to demonstrate the // diffieHellman.getGenerator() method // Destructure createDiffieHellman method from crypto const { createDiffieHellman } = require('crypto'); // Generate Alice's keys... const alice = createDiffieHellman(512); // Generate Alices's Prime const alicePrime = alice.getPrime(); // Generate Alice's Generator const aliceGenerator = alice.getGenerator() // Generate Alice's Key const aliceKey = alice.generateKeys(); // Generate Bob's keys... const bob = createDiffieHellman( alicePrime, aliceGenerator ); // Generate Bobs's Prime const bobPrime = bob.getPrime(); // Generate Bob's Generator const bobGenerator = bob.getGenerator() // Generate Bob's Key const bobKey = bob.generateKeys(); // Exchange and generate the secret... 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 }` ); // true Run the index.js file using the following command: node index.js Output: Is Symmetric key generation successful : trueReference: https://nodejs.org/api/crypto.html#crypto_diffiehellman_getgenerator_encoding Comment More infoAdvertise with us Next Article Node.js diffieHellman.getPublicKey() Method B braktim99 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Node.js-crypto-module Similar Reads Node.js diffieHellman.generateKeys() Method The diffieHellman.generateKeys() method is an inbuilt application programming interface of class DiffieHellman within the crypto module which is used to generate private and public key value of DiffieHellman (dh) object. Syntax: diffieHellman.generateKeys([encoding])Parameters: This method takes enc 3 min read Node.js diffieHellman.getPrime() Method The diffieHellman.getPrime() method is an inbuilt application programming interface of class diffieHellman within the crypto module which is used to get the prime of DiffieHellman (dh) object. Syntax: diffieHellman.getPrime([encoding])Parameters: This method takes encoding as a parameter. Return Va 2 min read Node.js diffieHellman.getPrivateKey() Method The diffieHellman.getPrivateKey() method is an inbuilt application programming interface of class DiffieHellman within the crypto module which is used to return the private key of dh object. Syntax: diffieHellman.getPrivateKey([encoding])Parameters: This method takes the encoding as a parameter. Ret 2 min read Node.js diffieHellman.getPublicKey() Method The diffieHellman.getPublicKey() method is an inbuilt application programming interface of class DiffieHellman within the crypto module which is used to return the public key of dh object. Syntax: diffieHellman.getPublicKey([encoding])Parameters: This method takes the encoding as a parameter. Retur 2 min read Node.js crypto.createDiffieHellman(primeLength, generator) Method The crypto.createDiffieHellman() method is used to create a Diffie-Hellman key exchange object. Also, creates prime of primeLength bits with the help of an optional specific numeric generator. Moreover, if the generator is not defined, then the value 2 is used. Syntax: crypto.createDiffieHellman( p 2 min read Node.js crypto.createDiffieHellmanGroup() Method The crypto.createDiffieHellmanGroup() method is an inbuilt application programming interface of the crypto module which is used to create a DiffieHellmanGroup. Syntax: crypto.createDiffieHellmanGroup( name ) Parameters: This method accepts single parameters name which is of type string. Return Val 1 min read Like