简体   繁体   中英

How to Encode nodejs ecdh public key as pem

Unable to sign a file with nodejs crypto

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM