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.