admin管理员组文章数量:1352810
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 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.
Share Improve this question asked Dec 2, 2020 at 8:32 Abdul RehmanAbdul Rehman 891 silver badge9 bronze badges 2- 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 Commented Dec 2, 2020 at 8:37
- I came across this question when I was also attempting to transliterate the quoted snippet of code from Ruby to JavaScript. It es from Authenticating as a GitHub App, for those who may be interested. – Quintin Willison Commented Sep 27, 2021 at 3:09
2 Answers
Reset to default 5const 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.
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.
本文标签: javascriptextract private key from pem fileStack Overflow
版权声明:本文标题:javascript - extract private key from .pem file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743882993a2555541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论