- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node.js – diffieHellman.setPrivateKey() Method
The diffieHellman.setPrivateKey() sets the Diffie-Hellman generated private key. The private key will be a string if an encoding argument is provided. If no encoding is provided, the private key will be of type buffer.
Syntax
diffieHellman.setPrivateKey( privateKey, [encoding] )
Parameters
encoding - This parameter specifies the encoding of the private Key.
Example 1
Create a file with the name "privateKey.js" and copy the following code. After creating the file, use the command "node privateKey.js" to run this code as shown in the example below
// diffieHellman.setPrivateKey() Demo Example // Importing the crypto module const crypto = require('crypto') // Generating the key pairs(public & private) crypto.generateKeyPair('rsa', { modulusLength: 530, primeLength: 512, publicKeyEncoding: { type: 'spki', format: 'der' }, privateKeyEncoding: { type: 'pkcs8', format: 'der' } }, createDiffieHellman ) function createDiffieHellman(err, publicKey, privateKey){ // Initializing diffieHellman const dh = crypto.createDiffieHellman(512) // Setting the private key dh.setPrivateKey(privateKey) if( privateKey.equals(dh.getPrivateKey()) ) console.log(privateKey) console.log("DH private Key is set successfully") }
Output
It will produce the following output −
<Buffer 30 82 01 5e 02 01 00 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 04 82 01 48 30 82 01 44 02 01 00 02 43 03 07 b1 e8 13 90 8b 34 f2 d8 45 51 a6 85 1a ... > DH private Key is set successfully
Example 2
Let's take a look at one more example
// diffieHellman.setPrivateKey() Demo Example // Importing the crypto module const crypto = require('crypto') // Generating the key pairs(public & private) crypto.generateKeyPair('dsa', { modulusLength: 530, primeLength: 512, publicKeyEncoding: { type: 'spki', format: 'der' }, privateKeyEncoding: { type: 'pkcs8', format: 'der' } }, createDiffieHellman ) function createDiffieHellman(err, publicKey, privateKey){ // Encoding the key in base64 privateKey = privateKey.toString('base64'); // Initializing diffieHellman const dh = crypto.createDiffieHellman( 512 ) // Setting the diffieHellman's privateKey dh.setPrivateKey( privateKey, 'base64' ) // Checking equality between both keys if( privateKey === dh.getPrivateKey('base64') ) console.log(privateKey) console.log( "Successfully assigned the private key" ) }
Output
MIHmAgEAMIHABgcqhkjOOAQBMIG0AkkA96JCvzbJkAmHtKSKljhJIomU8rKKtr2LF IaCiy+/BA/WlTqqn1+HCE7sW5RcVY7eVnv58u+9YMewXEwFDEdc0Po8b30akc+DAh 0A7ZkC4ZTiYz2AZ/3tjr9Z6jrPxh0ZVW3iwT/xWQJIT43jCxqeif/fnnYpt4S2VoT 3K82U/sgKtyYdpvPag5eJLB9ELTHA5w2E2ol4DSlsNbTXC4zUsUoZKorULHq3bCoy ev1ewoVsBB4CHBcHJboiyIg1ysR+gq0QIq/E0eKIIhjj4+OfpZY= Successfully assigned the private key
Advertisements
To Continue Learning Please Login
Login with Google