Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.getGenerator() Method
The diffieHellman.getGenerator() method returns the Diffie-Hellman generator in the specified encoding. A string is returned in case an encoding is passed, else a buffer is returned.
Syntax
diffieHellman.getGenerator([encoding])
Parameters
encoding - This parameter specifies the encoding of the return value.
Example 1
Create a file with the name "generator.js" and copy the following code. After creating the file, use the command "node generator.js" to run this code as shown in the example below:
// diffieHellman.getPrime() Demo Example
// Importing cryptoDiffieHellman from the crypto module
const { createDiffieHellman } = require('crypto');
// Initializing the diffieHellman object
const dh = createDiffieHellman(512);
// Generate DiffieHellman's Generator
// Defining generator
let dhGenerator = dh.getGenerator()
console.log('Buffer (when encoding is not specified) : ', dhGenerator)
// Encoding specified
// Return String
dhGenerator = dh.getGenerator('base64')
console.log('String (when encoding is specified) : ', dhGenerator)
Output
Buffer (when encoding is not specified) : <Buffer 02> String (when encoding is specified) : Ag==
Example 2
Let's take a look at one more example.
// diffieHellman.getGenerator() Demo Example
// Importing cryptoDiffieHellman from the crypto module
const { createDiffieHellman } = require('crypto');
// Generating keys for 'a'
const a = createDiffieHellman(512);
// Generating prime for 'a'
const primeA = a.getPrime();
// Generating a's generator
const generatorA = a.getGenerator()
// Generating a's generatorKeys
const keyA = a.generateKeys();
// Generating keys for b
const b = createDiffieHellman( primeA, generatorA );
// Generating prime for b
const primeB = b.getPrime();
// Generating b's generator
const generatorB = b.getGenerator()
// Generating b's generatorKeys
const keyB = b.generateKeys();
// Exchanging the secret
const secretA = a.computeSecret(keyB);
const secretB = b.computeSecret(keyA);
let isSymmetric =
secretA.toString('hex') == secretB.toString('hex')
console.log('Are keys Symmetric : ${ isSymmetric }')
Output
Are keys Symmetric : ${ isSymmetric }
Advertisements