【问题标题】:How to Encode nodejs ecdh public key as pem如何将nodejs ecdh公钥编码为pem
【发布时间】:2018-06-15 11:06:57
【问题描述】:

Unable to sign a file with nodejs crypto

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

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

【问题讨论】:

    标签: node.js sign cryptojs verify diffie-hellman


    【解决方案1】:

    web-push 库就是这样做的:

    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'
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多