📜  Node.js diffieHellman.getGenerator() 方法

📅  最后修改于: 2022-05-13 01:56:53.769000             🧑  作者: Mango

Node.js diffieHellman.getGenerator() 方法

diffieHellman.getGenerator()方法是 crypto 模块中diffieHellman类的内置应用程序编程接口,用于获取 DiffieHellman (dh) 对象的生成器值。

句法:

diffieHellman.getGenerator([encoding])

参数:该方法以编码为参数。

返回值:它以指定的编码返回 DiffieHellman 生成器值。如果没有提供编码,则返回 Buffer,否则返回 String。

示例 1:

index.js
// Node.js program to demonstrate the
// diffieHellman.getGenerator() method
  
// Destructure createDiffieHellman method from crypto
const { createDiffieHellman } = require('crypto');
  
// Instances of the DiffieHellman class
const dh = createDiffieHellman(512);
  
// Generate dh's Generator
  
// No encoding specified
// Return Buffer
let dhGenerator = dh.getGenerator()
console.log('\nIs Buffer return ( encoding not specified ) : ' +
    Buffer.isBuffer(dhGenerator)) // true
console.log('Return value :')
console.log(dhGenerator)
  
// Encoding specified 
// Return String
dhGenerator = dh.getGenerator('base64')
console.log('\nIs Buffer return ( encoding specified ): ' +
    Buffer.isBuffer(dhGenerator)) // true
console.log('Return value :')
console.log(dhGenerator)


index.js
// Node.js program to demonstrate the
// diffieHellman.getGenerator() method
  
// Destructure createDiffieHellman method from crypto
const { createDiffieHellman } = require('crypto');
  
// Generate Alice's keys...
const alice = createDiffieHellman(512);
  
// Generate Alices's Prime
const alicePrime = alice.getPrime();
  
// Generate Alice's Generator
const aliceGenerator =  alice.getGenerator()
  
// Generate Alice's Key
const aliceKey = alice.generateKeys();
  
// Generate Bob's keys...
const bob = createDiffieHellman( alicePrime, aliceGenerator );
  
// Generate Bobs's Prime
const bobPrime = bob.getPrime();
  
// Generate Bob's Generator
const bobGenerator =  bob.getGenerator()
  
// Generate Bob's Key
const bobKey = bob.generateKeys();
  
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
  
let isSymmetric = aliceSecret.toString('hex') == bobSecret.toString('hex')
  
console.log( `Is Symmetric key generation 
              successful : ${ isSymmetric }` ); // true


使用以下命令运行index.js文件:

node index.js

输出:

Is Buffer return ( encoding not specified ) : true
Return value :


Is Buffer return ( encoding specified ): false
Return value :
Ag==

示例 2:

index.js

// Node.js program to demonstrate the
// diffieHellman.getGenerator() method
  
// Destructure createDiffieHellman method from crypto
const { createDiffieHellman } = require('crypto');
  
// Generate Alice's keys...
const alice = createDiffieHellman(512);
  
// Generate Alices's Prime
const alicePrime = alice.getPrime();
  
// Generate Alice's Generator
const aliceGenerator =  alice.getGenerator()
  
// Generate Alice's Key
const aliceKey = alice.generateKeys();
  
// Generate Bob's keys...
const bob = createDiffieHellman( alicePrime, aliceGenerator );
  
// Generate Bobs's Prime
const bobPrime = bob.getPrime();
  
// Generate Bob's Generator
const bobGenerator =  bob.getGenerator()
  
// Generate Bob's Key
const bobKey = bob.generateKeys();
  
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
  
let isSymmetric = aliceSecret.toString('hex') == bobSecret.toString('hex')
  
console.log( `Is Symmetric key generation 
              successful : ${ isSymmetric }` ); // true

使用以下命令运行index.js文件:

node index.js

输出:

Is Symmetric key generation successful : true

参考: https://nodejs.org/api/crypto.html#crypto_diffiehellman_getgenerator_encoding