Node.js diffieHellman.setPublicKey() Method Last Updated : 11 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 parameters: publicKey: It is used to denote the private Key.encoding: It is used to denote the encoding of publicKey. If encoding is provided publicKey expected to be String otherwise Buffer, TypedArray, or DataView. Example 1: index.js // Node.js program to demonstrate the // diffieHellman.setPublicKey() Method const crypto = require('crypto') crypto.generateKeyPair('dh', { primeLength: 512, publicKeyEncoding: { type: 'spki', format: 'der' }, publicKeyEncoding: { type: 'pkcs8', format: 'der' } }, cb ) function cb(err, publicKey, publicKey) { // Create Diffie-Hellman instance const dh = crypto.createDiffieHellman(512) // Set the dh's publicKey dh.setPublicKey(publicKey) if (publicKey.equals(dh.getPublicKey())) console.log("DH public Key is set successfully") } Run index.js file using the following command node index.jsOutput: DH public Key is set successfullyExample 2: index.js // Node.js program to demonstrate the // diffieHellman.setPublicKey() Method const crypto = require('crypto') crypto.generateKeyPair( 'dh', { primeLength: 512 }, cb ) function cb(err, publicKey, publicKey) { // Export key from KeyObject publicKey = publicKey.export({ type: 'spki', format: 'der' }) // Encode key in base64 publicKey = publicKey.toString('base64'); // Create Diffie-Hellman instance const dh = crypto.createDiffieHellman(512) // Set the dh's publicKey dh.setPublicKey(publicKey, 'base64') if (publicKey === dh.getPublicKey('base64')) console.log("DH public Key is set successfully") } Run index.js file using the following command node index.jsOutput: DH public Key is set successfully Reference: https://nodejs.org/api/crypto.html#crypto_diffiehellman_setpublickey_publickey_encoding Comment More infoAdvertise with us Next Article Node.js ecdh.setPublicKey() Method B braktim99 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Node.js-crypto-module Similar Reads Node.js diffieHellman.setPrivateKey() Method 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 tw 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.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.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.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 Like