diffieHellman.getGenerator() 方法是crypto 模塊中diffieHellman 類的內置應用程序編程接口,用於獲取DiffieHellman (dh) 對象的生成器值。
用法:
diffieHellman.getGenerator([encoding])
參數:該方法將編碼作為參數。
返回值:它以指定的編碼返回 DiffieHellman 生成器值。如果沒有提供編碼,則返回 Buffer,否則返回 String。
範例1:
index.js
// Node.js program to demonstrate the
// diffieHellman.getGenerator() method
// Destructure createDiffieHellman method from crypto
const { createDiffieHellman } = require('crypto');
// Instances of the DiffieHellman class
const dh = createDiffieHellman(512);
// Generate dh's Generator
// No encoding specified
// Return Buffer
let dhGenerator = dh.getGenerator()
console.log('\nIs Buffer return ( encoding not specified ):' +
Buffer.isBuffer(dhGenerator)) // true
console.log('Return value:')
console.log(dhGenerator)
// Encoding specified
// Return String
dhGenerator = dh.getGenerator('base64')
console.log('\nIs Buffer return ( encoding specified ):' +
Buffer.isBuffer(dhGenerator)) // true
console.log('Return value:')
console.log(dhGenerator)
使用以下命令運行index.js文件:
node index.js
輸出:
Is Buffer return ( encoding not specified ):true Return value: <Buffer 02> Is Buffer return ( encoding specified ):false Return value: Ag==
範例2:
index.js
// Node.js program to demonstrate the
// diffieHellman.getGenerator() method
// Destructure createDiffieHellman method from crypto
const { createDiffieHellman } = require('crypto');
// Generate Alice's keys...
const alice = createDiffieHellman(512);
// Generate Alices's Prime
const alicePrime = alice.getPrime();
// Generate Alice's Generator
const aliceGenerator = alice.getGenerator()
// Generate Alice's Key
const aliceKey = alice.generateKeys();
// Generate Bob's keys...
const bob = createDiffieHellman( alicePrime, aliceGenerator );
// Generate Bobs's Prime
const bobPrime = bob.getPrime();
// Generate Bob's Generator
const bobGenerator = bob.getGenerator()
// Generate Bob's Key
const bobKey = bob.generateKeys();
// Exchange and generate the secret...
const aliceSecret = alice.computeSecret(bobKey);
const bobSecret = bob.computeSecret(aliceKey);
let isSymmetric = aliceSecret.toString('hex') == bobSecret.toString('hex')
console.log( `Is Symmetric key generation
successful:${ isSymmetric }` ); // true
使用以下命令運行index.js文件:
node index.js
輸出:
Is Symmetric key generation successful:true
參考:https://nodejs.org/api/crypto.html#crypto_diffiehellman_getgenerator_encoding
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
注:本文由純淨天空篩選整理自braktim99大神的英文原創作品 Node.js diffieHellman.getGenerator() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。