當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js ECDH用法及代碼示例


類:ECDH

添加於:v0.11.14

ECDH 類是用於創建橢圓曲線 Diffie-Hellman (ECDH) key 交換的實用程序。

ECDH 類的實例可以使用 crypto.createECDH() 函數創建。

import assert from 'node:assert';

const {
  createECDH
} = await import('node:crypto');

// Generate Alice's keys...
const alice = createECDH('secp521r1');
const aliceKey = alice.generateKeys();

// Generate Bob's keys...
const bob = createECDH('secp521r1');
const bobKey = bob.generateKeys();

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
// OKconst assert = require('node:assert');

const {
  createECDH,
} = require('node:crypto');

// Generate Alice's keys...
const alice = createECDH('secp521r1');
const aliceKey = alice.generateKeys();

// Generate Bob's keys...
const bob = createECDH('secp521r1');
const bobKey = bob.generateKeys();

// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);

assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
// OK

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 ECDH。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。