📜  Node.js diffieHellman.generateKeys() 方法

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

Node.js diffieHellman.generateKeys() 方法

diffieHellman.generateKeys()方法是加密模块中 DiffieHellman 类的内置应用程序编程接口,用于生成 DiffieHellman (dh) 对象的私钥和公钥值。

句法:

diffieHellman.generateKeys([encoding])

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

返回值:返回指定编码的 DiffieHellman 公钥。如果未提供编码,则返回 Buffer,否则返回 String。

示例 1:

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


index.js
// Node.js program to demonstrate the
// diffieHellman.generateKeys() 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('base64');
  
// 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('base64');
  
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey, 'base64', 'base64');
const bobSecret = bob.computeSecret(aliceKey, 'base64', 'base64');
  
let isSymmetric = aliceSecret == bobSecret
  
// true
console.log( `Is Symmetric key generation successful : ${ isSymmetric }` );


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

node index.js

输出:

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


Is Buffer return ( encoding specified ): false
Return value :
bReM6nQnW/SG+HL5c5MZiyeQhufYGxxQsmWaZrwsTUn2Rj61HAnl71y5jvxllapAO
mwITlF8/1Z67Nt7Lc8tqA==

示例 2:

index.js

// Node.js program to demonstrate the
// diffieHellman.generateKeys() 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('base64');
  
// 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('base64');
  
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey, 'base64', 'base64');
const bobSecret = bob.computeSecret(aliceKey, 'base64', 'base64');
  
let isSymmetric = aliceSecret == bobSecret
  
// true
console.log( `Is Symmetric key generation successful : ${ isSymmetric }` );

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

node index.js

输出:

Is Symmetric key generation successful : true

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