diff --git a/1to8.html b/1to8.html new file mode 100644 index 0000000..ce7d688 --- /dev/null +++ b/1to8.html @@ -0,0 +1,81 @@ + + + + + js-keygen: convert pkcs1 to pkcs8 + + + + + + + +
+

Convert PKCS1 private key to PKCS8 private key

+

From the command line you can run +
+ openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.pkcs1 -out private.pkcs8 -nocrypt +
We can do the same in javascript

+
No data is being sent to the server, everything happens within the context of this web page. +
+
+
+ +
+ +
+ +
+ +
+ +
+
To validate this pkcs8 record you can convert it baco pkcs1 through openssl: + openssl pkcs8 -inform PEM -outform PEM -in private.pkcs8 -out private.pkcs1 -nocrypt +
Made with + by + Patrick Roumanoff + + Fork me on GitHub + +
+ + + + + diff --git a/pkcs1To8.js b/pkcs1To8.js new file mode 100644 index 0000000..a3e8507 --- /dev/null +++ b/pkcs1To8.js @@ -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 };