Node.js diffieHellman.getPrime() Method Last Updated : 01 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Value: It returns the DiffieHellman prime value in the specified encoding. If encoding is not provided Buffer is returned else String is returned. Example 1: index.js // Node.js program to demonstrate the // diffieHellman.getPrime() method // Destructure createDiffieHellman method from crypto const { createDiffieHellman } = require('crypto'); // Instances of the DiffieHellman class const dh = createDiffieHellman(512); // Generate dh's Prime // No encoding specified // Return Buffer let dhPrime = dh.getPrime() console.log('\nIs Buffer return ( encoding not specified ) : ' + Buffer.isBuffer(dhPrime)) // true console.log('Return value :') console.log(dhPrime) // Encoding specified // Return String dhPrime = dh.getPrime('base64') console.log('\nIs Buffer return ( encoding specified ): ' + Buffer.isBuffer(dhPrime)) // true console.log('Return value :') console.log(dhPrime) Run the index.js file using the following command: node index.jsOutput: Is Buffer return ( encoding not specified ) : true Return value : <Buffer d9 10 5a 20 70 0e 9c 19 53 1d 74 bc 93 ac 9e 1d 00 65 cb 2b 7f 13 fd b5 67 cd ba 42 69 fc 2c 4c 44 5c 72 a7 45 26 2d d7 ff 2e ee c1 a9 ad 21 bf c4 3a ... 14 more bytes> Is Buffer return ( encoding specified ): false Return value : 2RBaIHAOnBlTHXS8k6yeHQBlyyt/E/21Z826Qmn8LExEXHKnRSYt1/ 8u7sGprSG/xDqF/zTVe5r9vQ+O0Q5PAw==Example 2: index.js // Node.js program to demonstrate the // diffieHellman.getPrime() 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.jsOutput: Is Symmetric key generation successful : trueReference: https://nodejs.org/api/crypto.html#crypto_diffiehellman_getprime_encoding Comment More infoAdvertise with us Next Article Node.js crypto.getDiffieHellman() Method B braktim99 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Node.js-crypto-module Similar Reads 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 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 p 2 min read 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 crypto.getDiffieHellman() Method The crypto.getDiffieHellman() method is used to create a predefined DiffieHellmanGroup key exchange object. Here, the favored groups are 'modp1', 'modp2', 'modp5', which are defined in RFC 2412 and 'modp14', 'modp15', 'modp16', 'modp17', 'modp18', defined in RFC 3526. Syntax: crypto.getDiffieHellman 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