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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
