diff --git a/README.md b/README.md index ccb7af7..cd7dec9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ and [Greenlock.js](https://git.coolaj86.com/coolaj86/greenlock.js) RSA tools. Lightweight. Zero Dependencies. Universal compatibility. * [x] PEM-to-JWK -* [ ] JWK-to-PEM (in progress) +* [x] JWK-to-PEM * [x] SSH "pub" format - `public: 'true'`: If a private key is used as input, a private key will be output. If you'd like to output a public key instead you can pass `public: true`. - - Testing ------- diff --git a/lib/asn1.js b/lib/asn1.js index c217aef..0200a01 100644 --- a/lib/asn1.js +++ b/lib/asn1.js @@ -204,7 +204,7 @@ ASN1.tpl = function (asn1) { console.log(vars.join('\n') + '\n'); console.log(); console.log('function buildSchema(opts) {'); - console.log('\treturn ' + str.slice(3) + ';'); + console.log('\treturn Enc.hexToBuf(' + str.slice(3) + ');'); console.log('}'); }; diff --git a/lib/x509.js b/lib/x509.js index 5cac827..4df5610 100644 --- a/lib/x509.js +++ b/lib/x509.js @@ -114,3 +114,42 @@ x509.packPkcs1 = function (jwk) { , ASN1.UInt(Enc.base64ToHex(jwk.qi)) )); }; + +x509.packPkcs8 = function (jwk) { + if (!jwk.d) { + // Public RSA + return Enc.hexToBuf(ASN1('30' + , ASN1('30' + , ASN1('06', '2a864886f70d010101') + , ASN1('05') + ) + , ASN1.BitStr(ASN1('30' + , ASN1.UInt(Enc.base64ToHex(jwk.n)) + , ASN1.UInt(Enc.base64ToHex(jwk.e)) + )) + )); + } + + // Private RSA + return Enc.hexToBuf(ASN1('30' + , ASN1.UInt('00') + , ASN1('30' + , ASN1('06', '2a864886f70d010101') + , ASN1('05') + ) + , ASN1('04' + , ASN1('30' + , ASN1.UInt('00') + , ASN1.UInt(Enc.base64ToHex(jwk.n)) + , ASN1.UInt(Enc.base64ToHex(jwk.e)) + , ASN1.UInt(Enc.base64ToHex(jwk.d)) + , ASN1.UInt(Enc.base64ToHex(jwk.p)) + , ASN1.UInt(Enc.base64ToHex(jwk.q)) + , ASN1.UInt(Enc.base64ToHex(jwk.dp)) + , ASN1.UInt(Enc.base64ToHex(jwk.dq)) + , ASN1.UInt(Enc.base64ToHex(jwk.qi)) + ) + ) + )); +}; +x509.packSpki = x509.packPkcs8; diff --git a/package.json b/package.json index 1d3764b..754a472 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rasha", - "version": "0.8.2", + "version": "1.0.0", "description": "PEM-to-JWK and JWK-to-PEM for RSA keys in a lightweight, zero-dependency library focused on perfect universal compatibility.", "homepage": "https://git.coolaj86.com/coolaj86/rasha.js", "main": "index.js", @@ -26,14 +26,14 @@ "keywords": [ "zero-dependency", "PEM-to-JWK", + "JWK-to-PEM", "RSA", "2048", "4096", "asn1", - "x509" - ], - "xkeywords": [ - "JWK-to-PEM" + "x509", + "JWK-to-SSH", + "PEM-to-SSH" ], "author": "AJ ONeal (https://coolaj86.com/)", "license": "MPL-2.0" diff --git a/test.sh b/test.sh index ebe8d47..734619a 100755 --- a/test.sh +++ b/test.sh @@ -37,16 +37,16 @@ node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.json pkcs1 > ./fixtures/pub-rsa-20 diff ./fixtures/pub-rsa-2048.pkcs1.pem ./fixtures/pub-rsa-2048.pkcs1.1.pem -#echo "" -#echo "" -#echo "Testing JWK-to-PEM PKCS#8" -#echo "" +echo "" +echo "" +echo "Testing JWK-to-PEM PKCS#8" +echo "" # -#node bin/rasha.js ./fixtures/privkey-rsa-2048.jwk.json pkcs8 > ./fixtures/privkey-rsa-2048.pkcs8.1.pem -#diff ./fixtures/privkey-rsa-2048.pkcs8.pem ./fixtures/privkey-rsa-2048.pkcs8.1.pem +node bin/rasha.js ./fixtures/privkey-rsa-2048.jwk.json pkcs8 > ./fixtures/privkey-rsa-2048.pkcs8.1.pem +diff ./fixtures/privkey-rsa-2048.pkcs8.pem ./fixtures/privkey-rsa-2048.pkcs8.1.pem # -#node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.json spki > ./fixtures/pub-rsa-2048.spki.1.pem -#diff ./fixtures/pub-rsa-2048.pski.pem ./fixtures/pub-rsa-2048.spki.1.pem +node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.json spki > ./fixtures/pub-rsa-2048.spki.1.pem +diff ./fixtures/pub-rsa-2048.spki.pem ./fixtures/pub-rsa-2048.spki.1.pem echo ""