DiffieHellman 类是一个用来创建 Diffie-Hellman 键交换的工具。

    DiffieHellman 类的实例可以使用 [crypto.createDiffieHellman()][] 方法。

    1. const crypto = require('crypto');
    2. const assert = require('assert');
    3. // 生成 Alice 的密钥。
    4. const alice = crypto.createDiffieHellman(2048);
    5. const aliceKey = alice.generateKeys();
    6. // 生成 Bob 的密钥。
    7. const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
    8. const bobKey = bob.generateKeys();
    9. // 交换并生成密钥。
    10. const aliceSecret = alice.computeSecret(bobKey);
    11. const bobSecret = bob.computeSecret(aliceKey);
    12. // 完成。
    13. assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));