如何将Node.js ECDH公钥编码为Pem [英] How to Encode nodejs ecdh public key as pem

查看:386
本文介绍了如何将Node.js ECDH公钥编码为Pem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使用nodejs crypto签署文件

我正在尝试使用带有ECDH公钥的verify.verify()方法来验证在此线程中创建的签名文档。因此,我想我必须将原始公钥格式化为有效的PEM。

I am trying to verify a signed document created like in this thread using the method verify.verify() with the ECDH public key. Therefore, i guess, i have to format the raw public key into valid PEM.

我将如何使用ans1.js和bn.js模块来做到这一点?

How would i do that using the ans1.js and bn.js module?

推荐答案

这是 web-push 库可以做到:

This is how web-push library does it:

const asn1 = require('asn1.js');

const ECPrivateKeyASN = asn1.define('ECPrivateKey', function() {
  this.seq().obj(
    this.key('version').int(),
    this.key('privateKey').octstr(),
    this.key('parameters').explicit(0).objid()
      .optional(),
    this.key('publicKey').explicit(1).bitstr()
      .optional()
  );
});

function toPEM(key) {
  return ECPrivateKeyASN.encode({
    version: 1,
    privateKey: key,
    parameters: [1, 2, 840, 10045, 3, 1, 7] // prime256v1
  }, 'pem', {
    label: 'EC PRIVATE KEY'
  });
}

这篇关于如何将Node.js ECDH公钥编码为Pem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆