📜  Node.js diffieHellman.computeSecret() 方法

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

Node.js diffieHellman.computeSecret() 方法

diffieHellman.computeSecret()方法是加密模块中 DiffieHellman 类的内置应用程序编程接口,用于计算 DiffieHellman (dh) 密钥。

句法:

diffieHellman.computeSecret(otherPublicKey[, 
        inputEncoding][, outputEncoding])

参数:此方法接受以下参数。

  • otherPublicKey:该方法以对方的公钥为第一个参数。
  • inputEncoding:指定otherPublicKey的编码。如果未提供otherPublicKey ,则应为 Buffer、TypedArray 或 DataView。
  • outputEncoding:如果提供,则返回 String,否则返回 Buffer。

返回值:此方法返回 diffieHellman 共享密钥。

示例 1:

index.js
// Node.js program to demonstrate the
// diffieHellman.computeSecret() method
  
const crypto = require('crypto')
  
// Instance of diffieHellman class...
const alice = crypto.createDiffieHellman(512);
const bob = crypto.createDiffieHellman(
        alice.getPrime(), alice.getGenerator());
  
// Generate otherPublicKey
// Pass 'base64' as encoding parameter
const aliceKey = alice.generateKeys('base64');
const bobKey = bob.generateKeys('base64');
  
// Exchange and generate the secret...
// inputEncoding = 'base64' because otherPublicKey
// is 'base64' encoded
// Return value should be 'hex' encoded because 
// outputEncoding = 'hex' 
const aliceSecret = alice.computeSecret(bobKey, 'base64', 'hex');
const bobSecret = bob.computeSecret(aliceKey, 'base64', 'hex');
  
if( aliceSecret === bobSecret ) 
    console.log(`Symmetric key : ${ aliceSecret }`)


index.js
// Node.js program to demonstrate the
// diffieHellman.computeSecret() method
  
const crypto = require('crypto')
  
// Instances of diffieHellman class
const alice = crypto.createDiffieHellman('modp15');
const bob = crypto.createDiffieHellman('modp15');
  
// Pass 'base64' as encoding parameter
const aliceKey = alice.generateKeys('base64');
  
// Pass 'hex' as encoding parameter
const bobKey = bob.generateKeys('hex');
  
// Pass inputEncoding = 'hex' because Bob's key 
// 'hex' encocded
// No outputEncoding provided
// Return Buffer 
const aliceSecret = alice.computeSecret(bobKey, 'hex');
  
// Pass inputEncoding = 'base64' because Alice's
// key 'base64' encocded 
// No outputEncoding provided
// Return Buffer
const bobSecret = bob.computeSecret(aliceKey, 'base64');
  
const isSymmetric = aliceSecret.equals(bobSecret)
  
if ( isSymmetric )
    console.log('Symmetric key : ', aliceSecret)


Javascript
// Node.js program to demonstrate the
// diffieHellman.computeSecret() method
  
const crypto = require('crypto')
  
// Instances of diffieHellman class
const alice = crypto.createDiffieHellman('modp15');
const bob = crypto.createDiffieHellman('modp15');
  
// Generate Key
alice.generateKeys();
bob.generateKeys();
  
// Compute Secret
// inputEncoding is null
// otherPublicKey expected to be Buffer 
const aliceSecret = alice.computeSecret(
        bob.getPublicKey() , null, 'base64');
  
const bobSecret = bob.computeSecret(
        alice.getPublicKey(), null, 'base64');
  
if (aliceSecret === bobSecret)
    console.log(`Symmetric key : ${ aliceSecret }`)


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

node index.js

输出:

Symmetric key : da884ccb0e24bf7e748f66998550b
f21f96b887e1f936478cdbc63b7806bd2403fd3aa28e5dbf58
bbabeb6f829dd86453eb0985b5ff593fcf7a8e1da20256b2a

示例 2:再举一个不提供outputEncoding 的示例。

index.js

// Node.js program to demonstrate the
// diffieHellman.computeSecret() method
  
const crypto = require('crypto')
  
// Instances of diffieHellman class
const alice = crypto.createDiffieHellman('modp15');
const bob = crypto.createDiffieHellman('modp15');
  
// Pass 'base64' as encoding parameter
const aliceKey = alice.generateKeys('base64');
  
// Pass 'hex' as encoding parameter
const bobKey = bob.generateKeys('hex');
  
// Pass inputEncoding = 'hex' because Bob's key 
// 'hex' encocded
// No outputEncoding provided
// Return Buffer 
const aliceSecret = alice.computeSecret(bobKey, 'hex');
  
// Pass inputEncoding = 'base64' because Alice's
// key 'base64' encocded 
// No outputEncoding provided
// Return Buffer
const bobSecret = bob.computeSecret(aliceKey, 'base64');
  
const isSymmetric = aliceSecret.equals(bobSecret)
  
if ( isSymmetric )
    console.log('Symmetric key : ', aliceSecret)

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

node index.js

输出:

Symmetric key :  

示例 3:另一个不提供inputEncoding 的示例。将 inputEncoding作为null传递。

文件名:index.js

Javascript

// Node.js program to demonstrate the
// diffieHellman.computeSecret() method
  
const crypto = require('crypto')
  
// Instances of diffieHellman class
const alice = crypto.createDiffieHellman('modp15');
const bob = crypto.createDiffieHellman('modp15');
  
// Generate Key
alice.generateKeys();
bob.generateKeys();
  
// Compute Secret
// inputEncoding is null
// otherPublicKey expected to be Buffer 
const aliceSecret = alice.computeSecret(
        bob.getPublicKey() , null, 'base64');
  
const bobSecret = bob.computeSecret(
        alice.getPublicKey(), null, 'base64');
  
if (aliceSecret === bobSecret)
    console.log(`Symmetric key : ${ aliceSecret }`)

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

node index.js

输出:

Symmetric key : abLoALX9

参考:

https://nodejs.org/api/crypto.html#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding