title: NodeJS Generate Valid PEM keys for Signing and Verifying messages
body: <h2>Context</h2>
  
  <p>From the NodeJS documentation on TLS/SSL for Node v10.9.0 (2018-AUG)</p>
  
  <p><a href="https://nodejs.org/api/tls.html#tls_tls_ssl_concepts" rel="noreferrer">https://nodejs.org/api/tls.html#tls_tls_ssl_concepts</a></p>
  
  <pre><code>openssl genrsa -out ryans-key.pem 2048
  </code></pre>
  
  <p>Will produce:</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  base64 encoded magic here...
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>Which I can then successfully use the <code>Sign</code> class to cryptographically sign a message:</p>
  
  <p><a href="https://nodejs.org/api/crypto.html#crypto_class_sign" rel="noreferrer">https://nodejs.org/api/crypto.html#crypto_class_sign</a></p>
  
  
  
  <pre><code>const crypto = require('crypto');
  const sign = crypto.createSign('RSA-SHA256');
  
  sign.update('some data to sign');
  
  const privateKey = `Insert magic value from above`;
  console.log(sign.sign(privateKey, 'base64'));
  </code></pre>
  
  <p>I have tried the following with no success:</p>
  
  
  
  <pre><code>const crypto = require('crypto');
  const dhke = crypto.createDiffieHellman(2048);
  dhke.generateKeys();
  const private_pem = `-----BEGIN RSA PRIVATE KEY-----
  ${dhke.getPrivateKey('base64')}
  -----END RSA PRIVATE KEY-----`;
  console.log(private_pem);
  
  const sign = crypto.createSign('RSA-SHA256');
  sign.update('some data to sign');
  
  const signature = sign.sign(private_pem, 'base64');
  console.log(signature);
  </code></pre>
  
  <p>Getting the following error:</p>
  
  <pre><code>Error: error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long
      at Sign.sign (internal/crypto/sig.js:84:26)
  ...
  </code></pre>
  
  <h2>The Question</h2>
  
  <p>How do I use the <code>crypto</code> library in NodeJS to achieve what <code>openssl</code> command line tool is performing (or another NPM module) to create a valid PEM formatted public/private key-pair which is required by the <code>Sign</code> class?</p>
  
  <h2>Similar unresolved questions</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/45363705/generate-key-for-signing-within-nodejs">Generate Key for signing within nodejs</a></li>
  </ul>
  
  <h1>Solution</h1>
  
  <p>Here is the start to finish working solution thanks to <a href="https://stackoverflow.com/a/53173811/622276">the accepted answer</a> from <a href="https://stackoverflow.com/users/8340438/jacobtdc">JacobTDC</a> where NodeJS v10.12.0 added this feature.</p>
  
  
  
  <pre><code>const crypto = require('crypto'); const sign = crypto.createSign('RSA-SHA256');
  
  sign.update('some data to sign');
  
  // $ openssl genrsa -out ryans-key.pem 2048 
  // const privateKey = `Insert magic value from above`;
  
  const { generateKeyPairSync } = require('crypto'); 
  const { publicKey, privateKey } = generateKeyPairSync('rsa', 
  {   modulusLength: 2048,  // the length of your key in bits   
      publicKeyEncoding: {
        type: 'spki',       // recommended to be 'spki' by the Node.js docs
        format: 'pem'   
      },   
      privateKeyEncoding: {
        type: 'pkcs8',      // recommended to be 'pkcs8' by the Node.js docs
        format: 'pem',
        //cipher: 'aes-256-cbc',   // *optional*
        //passphrase: 'top secret' // *optional*   
    } 
  }); 
  console.log(privateKey); 
  console.log(sign.sign(privateKey, 'base64'));
  </code></pre>