DiffieHellman 类
DiffieHellman
类是用于创建 Diffie-Hellman 密钥交换的实用工具。
可以使用 crypto.createDiffieHellman()
函数创建 DiffieHellman
类的实例。
const crypto = require('crypto');
const assert = require('assert');
// 生成 Alice 的密钥...
const alice = crypto.createDiffieHellman(2048);
const aliceKey = alice.generateKeys();
// 生成 Bob 的密钥...
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys();
// 交换并生成密钥...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
// OK
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
The DiffieHellman
class is a utility for creating Diffie-Hellman key
exchanges.
Instances of the DiffieHellman
class can be created using the
crypto.createDiffieHellman()
function.
const crypto = require('crypto');
const assert = require('assert');
// Generate Alice's keys...
const alice = crypto.createDiffieHellman(2048);
const aliceKey = alice.generateKeys();
// Generate Bob's keys...
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const bobKey = bob.generateKeys();
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
// OK
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));