1
votes

I want my code to extract private key from my pem file .

My pem file looks like this -> -----BEGIN RSA PRIVATE KEY----- some encrypted code -----END RSA PRIVATE KEY----- .

I have the same code in ruby but i'm not able to do this in javascript.

I have the same code in ruby but i'm not able to do this in javascript

2
Please add your code as text to your question (and pay attention to code formatting). There are many good reasons, why images of code are not a good idea. - jps
I came across this question when I was also attempting to transliterate the quoted snippet of code from Ruby to JavaScript. It comes from Authenticating as a GitHub App, for those who may be interested. - Quintin Willison

2 Answers

2
votes
const fs = require("fs");

var myKey = fs.readFileSync("mykey.pem", "utf8").replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "").trim();

console.log("My key is: ", myKey);

Keep in mind, this will only work if there is one key in the file instead of a list of keys, but you should get the gist.

0
votes

For those looking to Authenticate as a GitHub App, in a Node.js 14 context:

const fs = require('fs');
const crypto = require('crypto');
const privatePem = fs.readFileSync('github-app-private-key.pem');
const privateKey = crypto.createPrivateKey({
  key: privatePem,
});

I was then able to pass privateKey to jose SignJWT's sign method.