crypto:如何生成ECDH PEM?

时间:2018-06-26 15:15:47

标签: node.js

我是否正确假设可以仅将base64编码的缓冲区包装在页眉和页脚中就可以将Node.js生成的ECDH密钥格式化为PEM?

const { ECDH } = require('crypto')
const ecdh = ECDH('secp256k1')
ecdh.generateKeys()

const publicKey = ecdh.getPublicKey('buffer', 'uncompressed')
const privateKey = ecdh.getPrivateKey('buffer', 'uncompressed')

const publicPem = `-----BEGIN PUBLIC KEY-----\n${publicKey.toString('base64')}\n-----END PUBLIC KEY-----\n`
const privatePem = `-----BEGIN EC PRIVATE KEY-----\n${privateKey.toString('base64')}\n-----END EC PRIVATE KEY-----\n`
console.log('==================================')
console.log(privatePem)
console.log(publicPem)
console.log('==================================')

0 个答案:

没有答案