crypto.getDiffieHellman()方法用於創建預定義的DiffieHellmanGroup key 交換對象。在這裏,最喜歡的組是RFC 2412中定義的“ modp1”,“ modp2”,“ modp5”和RFC 3526中定義的“ modp14”,“ modp15”,“ modp16”,“ modp17”,“ modp18”。
用法:
crypto.getDiffieHellman( groupName )
參數:此方法接受字符串類型的單個參數groupName。
返回類型:它返回DiffieHellmanGroup key 交換對象。
以下示例說明了Node.js中crypto.getDiffieHellman()方法的使用:
範例1:
// Node.js program to demonstrate the
// crypto.getDiffieHellman() method
// Including crypto module
const crypto = require('crypto');
// Calling getDiffieHellman method
// with its parameter groupName
const diffiehellmangrp = crypto.getDiffieHellman('modp14');
// Prints DiffieHellmanGroup key exchange object
console.log("Key exchange object:", diffiehellmangrp);
輸出:
Key exchange object: DiffieHellmanGroup { _handle:{ verifyError:[Getter] }, verifyError:0 }
範例2:
// Node.js program to demonstrate the
// crypto.getDiffieHellman() method
// Including crypto module
const crypto = require('crypto');
// Calling two getDiffieHellman method
// with its parameter, groupName
const diffiehellmangrp1 = crypto.getDiffieHellman('modp14');
const diffiehellmangrp2 = crypto.getDiffieHellman('modp14');
// Generating keys
diffiehellmangrp1.generateKeys();
diffiehellmangrp2.generateKeys();
// Computing secret
const diffiehellmangrp1sc = diffiehellmangrp1
.computeSecret(diffiehellmangrp2.getPublicKey(), null, 'hex');
const diffiehellmangrp2sc = diffiehellmangrp2
.computeSecret(diffiehellmangrp1.getPublicKey(), null, 'hex');
// Checking if both the secrets are same or not
console.log(diffiehellmangrp1sc === diffiehellmangrp2sc);
輸出:
true
參考: https://nodejs.org/api/crypto.html#crypto_crypto_getdiffiehellman_groupname
相關用法
- Node.js GM quality()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM chop()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM paint()用法及代碼示例
- Node.js GM flop()用法及代碼示例
- Node.js GM segment()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | crypto.getDiffieHellman() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。