Node.js diffieHellman.setPrivateKey() Method Last Updated : 28 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The diffieHellman.setPrivateKey() method is an inbuilt application programming interface of class DiffieHellman (dh) within the crypto module which is used to set the private key of dh object. Syntax: diffieHellman.setPrivateKey(privateKey[, encoding])Parameters: This method accepts the following two parameters: privateKey: It is used to denote the private Key.encoding: It is used to denote the encoding of privateKey. If encoding is provided privateKey expected to be String otherwise Buffer, TypedArray, or DataView. Example 1: index.js // Node.js program to demonstrate the // diffieHellman.setPrivateKey() Method const crypto = require('crypto') // Generate DH Key pair crypto.generateKeyPair('dh', { primeLength: 512, publicKeyEncoding: { type: 'spki', format: 'der' }, privateKeyEncoding: { type: 'pkcs8', format: 'der' } }, cb ) function cb(err, publicKey, privateKey){ // Create Diffie-Hellman instance const dh = crypto.createDiffieHellman(512) // Set the dh's privateKey dh.setPrivateKey(privateKey) if( privateKey.equals(dh.getPrivateKey()) ) console.log("DH private Key is set successfully") } Run index.js file using the following command node index.jsOutput: DH private Key is set successfullyExample 2 : index.js // Node.js program to demonstrate the // diffieHellman.setPrivateKey() Method const crypto = require( 'crypto' ) crypto.generateKeyPair( 'dh', { primeLength: 512 }, cb ) function cb( err, publicKey, privateKey ){ // Export key from KeyObject privateKey = privateKey.export( {type: 'pkcs8', format: 'der'} ) // Encode key in base64 privateKey = privateKey.toString('base64'); // Create Diffie-Hellman instance const dh = crypto.createDiffieHellman( 512 ) // Set the dh's privateKey dh.setPrivateKey( privateKey, 'base64' ) if( privateKey === dh.getPrivateKey('base64') ) console.log( "DH private Key is set successfully" ) } Run index.js file using the following command node index.jsOutput: DH private Key is set successfully Reference: https://nodejs.org/api/crypto.html#crypto_ecdh_setprivatekey_privatekey_encoding Comment More infoAdvertise with us Next Article Node.js ecdh.setPrivateKey() Method B braktim99 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Node.js-crypto-module Similar Reads Node.js diffieHellman.setPublicKey() Method The diffieHellman.setPublicKey() method is an inbuilt application programming interface of class DiffieHellman (dh) within the crypto module which is used to set the public key of dh object. Syntax: diffieHellman.setPublicKey(publicKey[, encoding])Parameters: This method accepts the following two pa 2 min read Node.js ecdh.setPrivateKey() Method The ecdh.getPrivateKey() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to set the private key of the Elliptic Curve Diffie-Hellman (ECDH) object. The encoding of the key can be specified using the encoding parameter. An error would be thr 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 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 ecdh.setPublicKey() Method The ecdh.getPublicKey() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to set the public key of the Elliptic Curve Diffie-Hellman (ECDH) object. The encoding of the key can be specified using the encoding parameter. This method is not usua 3 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 Like