Node.js - diffieHellman.getPrivateKey() 方法


diffieHellman.getPrivateKey() 返回由指定的編碼生成的 Diffie-Hellman 私鑰。如果傳遞了編碼,則返回字串,否則將返回緩衝區。

語法

diffieHellman.getPrivateKey( [encoding] )

其中,encoding 是指定返回值編碼的引數。

示例 1

建立一個名為“privateKey.js” 的檔案並複製以下程式碼片段。建立檔案後,使用命令“node privateKey.js”來執行此程式碼,如下例所示 -

// diffieHellman.getPriateKey() Demo Example

// Importing the crypto module
const crypto = require( 'crypto' )

// Initializing the diffieHellman
const dh = crypto.createDiffieHellman( 512 );

// Generating Keys
dh.generateKeys()

// Getting buffer since encoding is not passed
let privateKey = dh.getPrivateKey()

// Checking if the key is a buffer
let isBuffer = Buffer.isBuffer( privateKey )

// Printing values
console.log( 'Private Key : ', privateKey )
console.log( 'Return value is Buffer :', isBuffer )

輸出

C:\home
ode>> node publicKey.js Private Key : <Buffer 60 38 6a c8 ec 31 f1 4e b7 10 87 4b 09 48 dd 45 bf 65 99 9e 05 fe ab 7f 1e 27 ee b3 7c a8 98 30 18 ee 9e 77 e5 be 65 39 f7 fa e1 bd b3 ce 5f c7 99 8e ... > Return value is Buffer : true

示例 2

我們再來看一個示例 -

// diffieHellman.getPriateKey() Demo Example

// Importing the crypto module
const crypto = require( 'crypto' )

// Initializing the diffieHellman
const dh = crypto.createDiffieHellman( 512 );

// Generating Keys
dh.generateKeys()

// Getting buffer since encoding is not passed
let privateKey = dh.getPrivateKey()

// Checking the key Type and printing values
console.log( 'Private Key : ', privateKey )
console.log( 'Return value is :', typeof privateKey )

輸出

C:\home
ode>> node computeSecret.js Private Key : <Buffer 44 05 93 8c b5 98 78 57 db 36 5c 27 ef 83 37 cb 76 dc b1 60 d7 8c 3f cc 20 d2 5f b2 71 71 c8 53 a2 ba e2 a2 73 10 7f 2f 0c b3 23 f3 b2 45 0e cd d6 86 ... > Return value is: object

更新於: 2022-01-17

114 次瀏覽

開啟你的 事業

完成課程認證

開始
廣告
© . All rights reserved.