Skip to content

Instantly share code, notes, and snippets.

@sweetpalma
Forked from canterberry/crypto.ec.pem.js
Created July 13, 2018 16:43
Show Gist options
  • Select an option

  • Save sweetpalma/65797cbbacd12cfa054469605b87e0a9 to your computer and use it in GitHub Desktop.

Select an option

Save sweetpalma/65797cbbacd12cfa054469605b87e0a9 to your computer and use it in GitHub Desktop.

Revisions

  1. Devin Canterberry created this gist Apr 26, 2018.
    18 changes: 18 additions & 0 deletions crypto.ec.pem.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // (Buffer is available in Node.js as a global, but we require it this way for compatibility)
    // See: https://nodejs.org/api/buffer.html#buffer_buffer
    const { Buffer } = require('buffer');
    const crypto = require('crypto');

    const keyPair = crypto.createECDH('secp256k1');

    keyPair.generateKeys();

    // Print the PEM-encoded private key
    console.log(`-----BEGIN PRIVATE KEY-----
    ${Buffer.from(`308184020100301006072a8648ce3d020106052b8104000a046d306b0201010420${keyPair.getPrivateKey('hex')}a144034200${keyPair.getPublicKey('hex')}`, 'hex').toString('base64')}
    -----END PRIVATE KEY-----`);

    // Print the PEM-encoded public key
    console.log(`-----BEGIN PUBLIC KEY-----
    ${Buffer.from(`3056301006072a8648ce3d020106052b8104000a034200${keyPair.getPublicKey('hex')}`, 'hex').toString('base64')}
    -----END PUBLIC KEY-----`);