Node.js diffieHellman.getPrivateKey() 方法
diffieHellman.getPrivateKey()方法是加密模块中DiffieHellman类的内置应用程序编程接口,用于返回dh对象的私钥。
句法:
diffieHellman.getPrivateKey([encoding])
参数:此方法将编码作为参数。
返回值:返回 diffieHellman 私钥。如果指定了编码,则返回一个字符串,否则返回一个 Buffer。
示例 1:
index.js
// Node.js program to demonstrate the
// diffieHellman.getPrivateKey() Method
const crypto = require( 'crypto' )
// Instance of diffieHellman class
const dh = crypto.createDiffieHellman( 512 );
// Generate Keys
dh.generateKeys()
// Without encoding, return Buffer
let privateKey = dh.getPrivateKey()
let isBuffer = Buffer.isBuffer( privateKey )
console.log( 'Private Key : ', privateKey )
console.log( 'Return value is Buffer :', isBuffer )
index.js
// Node.js program to demonstrate the
// diffieHellman.getPrivateKey() Method
const crypto = require( 'crypto' )
// Instance of diffieHellman class
const dh = crypto.createDiffieHellman( 512 );
// Generate Keys
dh.generateKeys()
// Pass 'base64' as encoding, return String
let privateKey = dh.getPrivateKey( 'base64' )
console.log( 'Private Key : ', privateKey )
console.log( 'Return value is :', typeof privateKey )
运行index.js 文件 使用以下命令:
node index.js
输出:
Private Key :
Return value is Buffer : true
示例 2:
index.js
// Node.js program to demonstrate the
// diffieHellman.getPrivateKey() Method
const crypto = require( 'crypto' )
// Instance of diffieHellman class
const dh = crypto.createDiffieHellman( 512 );
// Generate Keys
dh.generateKeys()
// Pass 'base64' as encoding, return String
let privateKey = dh.getPrivateKey( 'base64' )
console.log( 'Private Key : ', privateKey )
console.log( 'Return value is :', typeof privateKey )
运行index.js 文件 使用以下命令:
node index.js
输出:
Private Key : fG5wx60xqnulSgUaRM3J2IsBrtWN5ySbrph8mdzakZ/bMTfG+K
SY1P58sENdPjBbmoXHGy7RAfwFPa0kHHgslA==
Return value is : string
参考: https://nodejs.org/api/crypto.html#crypto_diffiehellman_getprivatekey_encoding