Node.js ecdh.getPrivateKey() Method Last Updated : 27 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The ecdh.getPrivateKey() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to get the private key of the Elliptic Curve Diffie-Hellman (ECDH) object. The encoding of the key can be specified using the encoding parameter and the format using the format parameter. Note: The keys have to be first generated using the generateKeys() method before they can be retrieved using the private key. Syntax: ecdh.getPrivateKey( encoding )Parameters: This method accepts a single parameter as mentioned above and described below: encoding: This is a string value that specifies the encoding of the return value. It is an optional parameter.format: It is a string that specifies the format of the keys. The value can be 'compressed' or 'uncompressed'. It is an optional parameter.Return Value: It returns the Elliptic Curve DiffieHellman private key in the specified encoding. When the encoding is not provided, it is returned as a Buffer, otherwise, a String is returned. The examples below demonstrate this method: Example 1: JavaScript const crypto = require('crypto'); // Generate an ECDH object for geekA const geekA = crypto.createECDH('secp521r1'); // Generate an ECDH object for geekB const geekB = crypto.createECDH('secp521r1'); // Generate the keys for both the geeks geekA.generateKeys(); geekB.generateKeys(); // Get the private key for geekA const geekAprivateKey = geekA.getPrivateKey(); console.log("Private Key of Geek A is:", geekAprivateKey); // Get the private key for geekB const geekBprivateKey = geekB.getPrivateKey(); console.log("Private Key of Geek B is:", geekBprivateKey); Output: Private Key of Geek A is: <Buffer b1 cd 2f e7 52 b4 8b a9 fe 2d 1c d8 6d 35 d9 7b 2f 2b 48 27 40 ac 35 3a 34 3a 63 5b cd 28 d0 5f 38 9b d6 ad 6c d3 a7 6e ed d5 9e 84 e6 e6 83 84 c4 cb ... 15 more bytes> Private Key of Geek B is: <Buffer 01 2f b2 ea a9 65 23 27 65 be ab 68 3a 6e 88 6e db c4 6a 65 9d 52 b7 9e 69 39 b5 5f 3a 87 55 3d f6 62 51 94 5e 24 48 62 96 dc 3b 4f 10 d2 30 99 03 4c ... 16 more bytes> Example 2: JavaScript const crypto = require('crypto'); // Generate an ECDH object for geekA const geekA = crypto.createECDH('secp521r1'); // Generate an ECDH object for geekB const geekB = crypto.createECDH('secp521r1'); // Generate the keys for both the geeks geekA.generateKeys(); geekB.generateKeys(); // Get the private key for geekA in base64 const geekAPrivateKey = geekA.getPrivateKey('base64'); console.log("Private Key of Geek A is:", geekAPrivateKey); // Get the private key for geekB in hex const geekBPrivateKey = geekB.getPrivateKey('hex'); console.log("Private Key of Geek B is:", geekBPrivateKey); Output: Private Key of Geek A is: AXZvAlAJi4U5gL0Hj+kkriNlH45k+UkIz27MD0x4jsbKTjEUcU8vTUFvcw3aLHt4lit+V1Jp6fQ/4MH2dFNlfpu6 Private Key of Geek B is: 0152465d0c729f46f1f78532fde54eb0218b491e5a87926120862b79de9084e7a78f604de4d4d9f4c31ab7819bf6cf8f0d75849781ad556fed947ad968e008c90940 Reference: https://nodejs.org/api/crypto.html#crypto_ecdh_getprivatekey_encoding Comment More infoAdvertise with us Next Article Node.js diffieHellman.getPrivateKey() Method S sayantanm19 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Node.js-crypto-module Similar Reads Node.js ecdh.generateKeys() Method The ecdh.generateKeys() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to generate private and public key values of the Elliptic Curve Diffie-Hellman (ECDH) object. It returns only the public key in the given format and encoding. Syntax: e 3 min read Node.js ecdh.getPublicKey() Method The ecdh.getPublicKey() method is an inbuilt application programming interface of class ECDH within the crypto module which is used to get the public key of the Elliptic Curve Diffie-Hellman (ECDH) object in the specified encoding. The encoding of the key can be specified using the encoding paramete 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 Node.js crypto.generateKeyPair() Method The crypto.generateKeyPair() method is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type. For example, the currently supported key types are RSA, DSA, EC, Ed25519, Ed448, X25519, X448, and DH. Moreover, if option's 4 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 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 Like