1to8
This commit is contained in:
parent
a3a078b963
commit
4bc108ac7e
2 changed files with 165 additions and 0 deletions
81
1to8.html
Normal file
81
1to8.html
Normal file
|
@ -0,0 +1,81 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>js-keygen: convert pkcs1 to pkcs8</title>
|
||||
<script>module = {};</script>
|
||||
<script src="pkcs1To8.js"></script>
|
||||
<link rel="stylesheet" href="js-keygen.css">
|
||||
<link rel="icon" type="image/png" href="key.png">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="content">
|
||||
<h1>Convert PKCS1 private key to PKCS8 private key</h1>
|
||||
<p>From the command line you can run
|
||||
<br>
|
||||
<code>openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.pkcs1 -out private.pkcs8 -nocrypt</code>
|
||||
<br>We can do the same in javascript</p>
|
||||
<br> No data is being sent to the server, everything happens within the context of this web page.
|
||||
<br>
|
||||
<hr>
|
||||
<div>
|
||||
<label for="key">PKCS1 private Key PEM:</label>
|
||||
<br>
|
||||
<textarea id="key" style="height: 300px;" spellcheck="false">-----BEGIN PRIVATE KEY-----
|
||||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCku91CnYnU+88p
|
||||
sDWthuLgMLZd1cWOMF8xV7KUNR/EqssNWObwdD/mrX6Z8t9MPl0T6jL8+EMHRcQn
|
||||
pIR8bgUf7WT7Kl9INe/TpoIVX1bfyenQIqJbFE4lPs3E0VJP45wHFFRK85ls08zI
|
||||
OVDmzYFt87upTiwPbxS5OIFUB16sxttKAdXhebtWtkVQPB9662OG2WRhVlJqXNc9
|
||||
wuWvb0XnbVUxF0ryhsJj24VR3cjHQtkDc22Vu8yUzWS3VtBSUdfzT1EwVXm1AGAz
|
||||
WhjO+W/7fvWYvR9uO7O+po0g4kbloL9lO1v+pMPVZvcv6VQqLbcgNc6fzAlfwis9
|
||||
a/uKgRZhAgMBAAECggEADtNBZF9dKifHSw8qpYVAIcEAiI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B7AaaOKY5n45sJKxGSliJdL2o0FealVXra3IpECgYA0YYrmCNXb2LISpJe3NLJI
|
||||
4UkMLsRwR4rnumsmbQl7u3NAjZtvsUWv+b8fayEbQii49RwfslJclrSAG/y0l/sA
|
||||
l+a6EbtPiu6HeP0nLYksP/yxSd1RfXWr2KEKQXv0DSRguQAmr5QyR6CMCJXvM/ul
|
||||
zB/5uOOZD9IzgautQqwIcQ==
|
||||
-----END PRIVATE KEY-----
|
||||
</textarea>
|
||||
</div>
|
||||
<button id="generate">Convert</button>
|
||||
<div>
|
||||
<label for="pem">PKCS8 private key PEM:</label>
|
||||
<br>
|
||||
<textarea id="pem" disabled style="height: 350px;" spellcheck="false"></textarea>
|
||||
</div>
|
||||
<br> To validate this pkcs8 record you can convert it baco pkcs1 through openssl:
|
||||
<code>openssl pkcs8 -inform PEM -outform PEM -in private.pkcs8 -out private.pkcs1 -nocrypt</code>
|
||||
<hr> Made with
|
||||
<span style="color:magenta;">♥</span> by
|
||||
<a href="http://blog.roumanoff.com">Patrick Roumanoff</a>
|
||||
<a href="https://github.com/PatrickRoumanoff/js-keygen">
|
||||
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67"
|
||||
alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png">
|
||||
</a>
|
||||
</div>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.querySelector("#generate").addEventListener("click", () => {
|
||||
document.querySelector("#pem").value = pkcs1To8(document.querySelector("#key").value)
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
84
pkcs1To8.js
Normal file
84
pkcs1To8.js
Normal file
|
@ -0,0 +1,84 @@
|
|||
/* eslint no-bitwise: 0 */
|
||||
|
||||
function wrap(text, len) {
|
||||
const length = len || 72;
|
||||
let result = "";
|
||||
for (let i = 0; i < text.length; i += length) {
|
||||
result += text.slice(i, i + length);
|
||||
result += "\n";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function pemPrivateKey(key) {
|
||||
return `-----BEGIN PRIVATE KEY-----\n${wrap(key, 64)}-----END PRIVATE KEY-----`;
|
||||
}
|
||||
|
||||
function stripPemFormatting(str) {
|
||||
return str
|
||||
.replace(/^-----BEGIN (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----$/m, "")
|
||||
.replace(/^-----END (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----$/m, "")
|
||||
.replace(/[\n\r]/g, "");
|
||||
}
|
||||
function arrayToPem(a) {
|
||||
return window.btoa(a.map(c => String.fromCharCode(c)).join(""));
|
||||
}
|
||||
|
||||
function stringToArray(s) {
|
||||
return s.split("").map(c => c.charCodeAt());
|
||||
}
|
||||
|
||||
function pemToArray(pem) {
|
||||
return stringToArray(window.atob(pem));
|
||||
}
|
||||
|
||||
const prefix = [
|
||||
0x30,
|
||||
0x82,
|
||||
0x04,
|
||||
0xbc,
|
||||
0x02,
|
||||
0x01,
|
||||
0x00,
|
||||
0x30,
|
||||
0x0d,
|
||||
0x06,
|
||||
0x09,
|
||||
0x2a,
|
||||
0x86,
|
||||
0x48,
|
||||
0x86,
|
||||
0xf7,
|
||||
0x0d,
|
||||
0x01,
|
||||
0x01,
|
||||
0x01,
|
||||
0x05,
|
||||
0x00,
|
||||
0x04,
|
||||
0x82,
|
||||
0x04,
|
||||
0xa6,
|
||||
];
|
||||
|
||||
function pkcs1To8(privateKeyPkcs1Pem) {
|
||||
const pem = stripPemFormatting(privateKeyPkcs1Pem);
|
||||
const privateKeyPkcs1Array = pemToArray(pem);
|
||||
const prefixPkcs8 = prefix.concat(privateKeyPkcs1Array);
|
||||
const privateKeyPkcs8Pem = arrayToPem(prefixPkcs8);
|
||||
const pkcs8Pem = pemPrivateKey(privateKeyPkcs8Pem);
|
||||
return pkcs8Pem;
|
||||
}
|
||||
|
||||
// crypto.subtle.importKey(
|
||||
// "spki",
|
||||
// keyTextBuffer,
|
||||
// {
|
||||
// name: "RSASSA-PKCS1-v1_5",
|
||||
// hash: { name: "SHA-256" },
|
||||
// },
|
||||
// true,
|
||||
// ["verify"]
|
||||
// );
|
||||
|
||||
module.exports = { pkcs1To8 };
|
Loading…
Reference in a new issue