From 4a436138dc7c8b5f3e2d84920b95d1c57f1a15e8 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 03:41:06 +0300 Subject: [PATCH 01/18] Fixed indentation --- rsa-pem-to-jwk.js | 88 +++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 117c18f..011c9b7 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -30,58 +30,58 @@ var objectAssign = require('object-assign'); * - rsaPemToJwk('...', {...}); */ module.exports = function rsaPemToJwk(pem, extraKeys, type) { - // Unpack the PEM - var key = rsaUnpack(pem); - if (key === undefined) { - return undefined; - } + // Unpack the PEM + var key = rsaUnpack(pem); + if (key === undefined) { + return undefined; + } - // Process parameters - if (typeof extraKeys === 'string') { - type = extraKeys; - extraKeys = {}; - } - type = type || (key.privateExponent !== undefined ? 'private' : 'public'); + // Process parameters + if (typeof extraKeys === 'string') { + type = extraKeys; + extraKeys = {}; + } + type = type || (key.privateExponent !== undefined ? 'private' : 'public'); - // Requested private JWK but gave a public PEM - if (type == 'private' && key.privateExponent === undefined) { - return undefined; - } + // Requested private JWK but gave a public PEM + if (type == 'private' && key.privateExponent === undefined) { + return undefined; + } - // Make the public exponent into a buffer of minimal size - var expSize = Math.ceil(Math.log(key.publicExponent) / Math.log(256)); - var exp = new Buffer(expSize); - var v = key.publicExponent; + // Make the public exponent into a buffer of minimal size + var expSize = Math.ceil(Math.log(key.publicExponent) / Math.log(256)); + var exp = new Buffer(expSize); + var v = key.publicExponent; - for (var i = expSize - 1; i >= 0; i--) { - exp.writeUInt8(v % 256, i); - v = Math.floor(v / 256); - } + for (var i = expSize - 1; i >= 0; i--) { + exp.writeUInt8(v % 256, i); + v = Math.floor(v / 256); + } - // The public portion is always present - var r = objectAssign({kty: 'RSA'}, extraKeys, { - n: base64url(key.modulus), - e: base64url(exp), - }); - - // Add private - if (type === 'private') { - objectAssign(r, { - d: base64url(key.privateExponent), - p: base64url(key.prime1), - q: base64url(key.prime2), - dp: base64url(key.exponent1), - dq: base64url(key.exponent2), - qi: base64url(key.coefficient), + // The public portion is always present + var r = objectAssign({kty: 'RSA'}, extraKeys, { + n: base64url(key.modulus), + e: base64url(exp), }); - } - return r; + // Add private + if (type === 'private') { + objectAssign(r, { + d: base64url(key.privateExponent), + p: base64url(key.prime1), + q: base64url(key.prime2), + dp: base64url(key.exponent1), + dq: base64url(key.exponent2), + qi: base64url(key.coefficient), + }); + } + + return r; }; function base64url(b) { - return b.toString('base64') - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=/g, ''); + return b.toString('base64') + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=/g, ''); } From 1c96bbfc125fa878af7b079d8d55491fc566e4ba Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 03:44:55 +0300 Subject: [PATCH 02/18] Removed dependency object-assign --- rsa-pem-to-jwk.js | 17 +++++++---------- test/rsa-pem-to-jwk.test.js | 7 +++---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 011c9b7..2ffad51 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -15,7 +15,6 @@ 'use strict'; var rsaUnpack = require('rsa-unpack'); -var objectAssign = require('object-assign'); /* * Parameters: @@ -59,24 +58,22 @@ module.exports = function rsaPemToJwk(pem, extraKeys, type) { } // The public portion is always present - var r = objectAssign({kty: 'RSA'}, extraKeys, { + return { + kty: 'RSA', + extraKeys, n: base64url(key.modulus), e: base64url(exp), - }); - // Add private - if (type === 'private') { - objectAssign(r, { + // Add private + ...type === 'private' && { d: base64url(key.privateExponent), p: base64url(key.prime1), q: base64url(key.prime2), dp: base64url(key.exponent1), dq: base64url(key.exponent2), - qi: base64url(key.coefficient), - }); + qi: base64url(key.coefficient) + } } - - return r; }; function base64url(b) { diff --git a/test/rsa-pem-to-jwk.test.js b/test/rsa-pem-to-jwk.test.js index 95be12a..05c02ef 100644 --- a/test/rsa-pem-to-jwk.test.js +++ b/test/rsa-pem-to-jwk.test.js @@ -20,7 +20,6 @@ var fs = require('fs'); var path = require('path'); var expect = require('chai').expect; -var objectAssign = require('object-assign'); var rsaPemToJwk = require('../rsa-pem-to-jwk'); @@ -51,13 +50,13 @@ describe('rsa-pem-to-jwk', function() { it('should return a public JWK with extra keys', function() { var jwk = rsaPemToJwk(publicPem, {use: 'sig'}); - expect(jwk).to.eql(objectAssign({}, expectedPublic, {use: 'sig'})); + expect(jwk).to.eql({...expectedPublic, use: 'sig'}); }); it('should return a private JWK with extra keys', function() { var jwk = rsaPemToJwk(privatePem, {use: 'sig'}); - expect(jwk).to.eql(objectAssign({}, expectedPrivate, {use: 'sig'})); + expect(jwk).to.eql({...expectedPrivate, use: 'sig'}); }); it('should return a public JWK from a private PEM', function() { @@ -70,7 +69,7 @@ describe('rsa-pem-to-jwk', function() { function() { var jwk = rsaPemToJwk(privatePem, {use: 'sig'}, 'public'); - expect(jwk).to.eql(objectAssign({}, expectedPublic, {use: 'sig'})); + expect(jwk).to.eql({...expectedPublic, use: 'sig'}); }); it('should fail to return a private JWK from a public PEM', function() { From 418e09905b8d3ecb7cf2e66d10fff2637f206415 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 03:46:34 +0300 Subject: [PATCH 03/18] Removed dependency object-assign from package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 05c2ee9..e4560c6 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "coveralls": "~2.11.2" }, "dependencies": { - "object-assign": "^2.0.0", "rsa-unpack": "0.0.6" } } From 1312ab170dbd0ca9975c89af86f83cbd4d190bd4 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 04:51:20 +0300 Subject: [PATCH 04/18] Fix --- rsa-pem-to-jwk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 2ffad51..82eec4a 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -60,7 +60,7 @@ module.exports = function rsaPemToJwk(pem, extraKeys, type) { // The public portion is always present return { kty: 'RSA', - extraKeys, + ...extraKeys, n: base64url(key.modulus), e: base64url(exp), From 775cd9dc06fb479b6b1154c2107cf28ea108d92f Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 06:34:35 +0300 Subject: [PATCH 05/18] Removed 'use strict' --- rsa-pem-to-jwk.js | 1 - 1 file changed, 1 deletion(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 82eec4a..27cf619 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -'use strict'; var rsaUnpack = require('rsa-unpack'); From d4091683e24349b9195251280ad4989bc6581762 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 06:56:39 +0300 Subject: [PATCH 06/18] Rewrited rsa unpack method --- rsa-pem-to-jwk.js | 122 ++++++++++++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 41 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 27cf619..7827b0e 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -13,7 +13,67 @@ * limitations under the License. */ -var rsaUnpack = require('rsa-unpack'); +function base64toArrayBuffer(base64) { + return typeof atob !== 'undefined' ? + Uint8Array.from(atob(base64), byte => byte.charCodeAt(0)) : // Browser API + new Uint8Array(Buffer.from(base64, 'base64')); // Node.js API +} + +function arrayBufferToBase64(buffer) { + return typeof btoa !== 'undefined' ? + btoa(String.fromCharCode(...new Uint8Array(buffer.buffer || buffer))) : // Browser API + Buffer(buffer.buffer || buffer).toString('base64'); // Node.js API +} + +function rsaPemUnpack(pemKey) { + pemKey = pemKey.trim().split("\n"); + + // Check and remove RSA key header/footer + let keyType = (/-----BEGIN RSA (PRIVATE|PUBLIC) KEY-----/.exec(pemKey.shift()) || [])[1]; + if (!keyType || !RegExp(`-----END RSA ${keyType} KEY-----`).exec(pemKey.pop())) { + throw Error('Headers not supported.'); + } + + // Check requested JWK and given PEM types + keyType = keyType.toLowerCase(); + if (type !== keyType) { + throw Error(`RSA type mismatch: requested ${type}, given ${keyType}.`); + } + type = keyType; + + // PEM base64 to ArrayBuffer + const derKey = new Uint8Array(base64toArrayBuffer(pemKey.join(''))); + + // DER reading offset + let offset = { + private: derKey[1] & 0x80 ? derKey[1] - 0x80 + 5 : 7, + public: derKey[1] & 0x80 ? derKey[1] - 0x80 + 2 : 2 + }[keyType]; + + function read() { + let s = derKey[offset + 1]; + + if (s & 0x80) { + let n = s - 0x80; + s = new DataView(derKey.buffer)[ ['getUint8', 'getUint16'][n - 1] ](offset + 2); + offset += n; + } + offset += 2; + + return derKey.slice(offset, offset += s); + } + + return { + modulus: read(), + publicExponent: read(), + privateExponent: read(), + prime1: read(), + prime2: read(), + exponent1: read(), + exponent2: read(), + coefficient: read() + }; +} /* * Parameters: @@ -27,57 +87,37 @@ var rsaUnpack = require('rsa-unpack'); * - rsaPemToJwk('...', 'private'); * - rsaPemToJwk('...', {...}); */ -module.exports = function rsaPemToJwk(pem, extraKeys, type) { - // Unpack the PEM - var key = rsaUnpack(pem); - if (key === undefined) { - return undefined; - } - +module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { // Process parameters if (typeof extraKeys === 'string') { type = extraKeys; extraKeys = {}; } - type = type || (key.privateExponent !== undefined ? 'private' : 'public'); - // Requested private JWK but gave a public PEM - if (type == 'private' && key.privateExponent === undefined) { - return undefined; - } - - // Make the public exponent into a buffer of minimal size - var expSize = Math.ceil(Math.log(key.publicExponent) / Math.log(256)); - var exp = new Buffer(expSize); - var v = key.publicExponent; + // Unpack the PEM + const key = rsaPemUnpack(pemKey); - for (var i = expSize - 1; i >= 0; i--) { - exp.writeUInt8(v % 256, i); - v = Math.floor(v / 256); + function base64Url(buffer) { + return arrayBufferToBase64(buffer) + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/g, ''); } - // The public portion is always present return { kty: 'RSA', ...extraKeys, - n: base64url(key.modulus), - e: base64url(exp), - - // Add private - ...type === 'private' && { - d: base64url(key.privateExponent), - p: base64url(key.prime1), - q: base64url(key.prime2), - dp: base64url(key.exponent1), - dq: base64url(key.exponent2), - qi: base64url(key.coefficient) + // The public portion is always present + n: base64Url(key.modulus), + e: base64Url(key.publicExponent), + // Read private part + ...type === "private" && { + d: base64Url(key.privateExponent), + p: base64Url(key.prime1), + q: base64Url(key.prime2), + dp: base64Url(key.exponent1), + dq: base64Url(key.exponent2), + qi: base64Url(key.coefficient) } - } + }; }; - -function base64url(b) { - return b.toString('base64') - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=/g, ''); -} From 8a5878672b75294c301756b09061fb3833ba4a87 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 06:57:24 +0300 Subject: [PATCH 07/18] Removed dependency rsa-unpack from package.json --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index e4560c6..1c6ebd5 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,5 @@ "mocha": "^2.0.1", "coveralls": "~2.11.2" }, - "dependencies": { - "rsa-unpack": "0.0.6" - } + "dependencies": {} } From d5af0902ae3daa011f99eebdb51bbcdcb8c160e7 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 06:58:48 +0300 Subject: [PATCH 08/18] Buffer fix --- rsa-pem-to-jwk.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 7827b0e..23ffbe9 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -21,8 +21,8 @@ function base64toArrayBuffer(base64) { function arrayBufferToBase64(buffer) { return typeof btoa !== 'undefined' ? - btoa(String.fromCharCode(...new Uint8Array(buffer.buffer || buffer))) : // Browser API - Buffer(buffer.buffer || buffer).toString('base64'); // Node.js API + btoa(String.fromCharCode(...new Uint8Array(buffer))) : // Browser API + Buffer.from(buffer).toString('base64'); // Node.js API } function rsaPemUnpack(pemKey) { From 1285ef70b9d41d0575502a28a91d7f063a1de6ae Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 06:59:14 +0300 Subject: [PATCH 09/18] Uint8Array fix --- rsa-pem-to-jwk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 23ffbe9..d198603 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -16,7 +16,7 @@ function base64toArrayBuffer(base64) { return typeof atob !== 'undefined' ? Uint8Array.from(atob(base64), byte => byte.charCodeAt(0)) : // Browser API - new Uint8Array(Buffer.from(base64, 'base64')); // Node.js API + Uint8Array.from(Buffer.from(base64, 'base64')); // Node.js API } function arrayBufferToBase64(buffer) { From 711d645966d9021c427c9a71d1b971918cb972de Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:01:17 +0300 Subject: [PATCH 10/18] Key convertion to string --- rsa-pem-to-jwk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index d198603..21800e8 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -26,7 +26,7 @@ function arrayBufferToBase64(buffer) { } function rsaPemUnpack(pemKey) { - pemKey = pemKey.trim().split("\n"); + pemKey = String(pemKey).trim().split("\n"); // Check and remove RSA key header/footer let keyType = (/-----BEGIN RSA (PRIVATE|PUBLIC) KEY-----/.exec(pemKey.shift()) || [])[1]; From 335978a59dd8d0c71325bd59425023567d5c1c12 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:03:42 +0300 Subject: [PATCH 11/18] Restructured code --- rsa-pem-to-jwk.js | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 21800e8..9dcf6a9 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -25,7 +25,26 @@ function arrayBufferToBase64(buffer) { Buffer.from(buffer).toString('base64'); // Node.js API } -function rsaPemUnpack(pemKey) { +/* + * Parameters: + * pem - string of PEM encoded RSA key + * extraKeys - custom keys to be included in JWK + * type - 'public' for JWK of only the public portion of the key and + * 'private' for a JWK of both the public and private portions + * + * Prototypes: + * - rsaPemToJwk('...', {...}, 'public'); + * - rsaPemToJwk('...', 'private'); + * - rsaPemToJwk('...', {...}); + */ +module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { + // Process parameters + if (typeof extraKeys === 'string') { + type = extraKeys; + extraKeys = {}; + } + + // Unpack the PEM pemKey = String(pemKey).trim().split("\n"); // Check and remove RSA key header/footer @@ -63,7 +82,7 @@ function rsaPemUnpack(pemKey) { return derKey.slice(offset, offset += s); } - return { + const key = { modulus: read(), publicExponent: read(), privateExponent: read(), @@ -73,29 +92,6 @@ function rsaPemUnpack(pemKey) { exponent2: read(), coefficient: read() }; -} - -/* - * Parameters: - * pem - string of PEM encoded RSA key - * extraKeys - custom keys to be included in JWK - * type - 'public' for JWK of only the public portion of the key and - * 'private' for a JWK of both the public and private portions - * - * Prototypes: - * - rsaPemToJwk('...', {...}, 'public'); - * - rsaPemToJwk('...', 'private'); - * - rsaPemToJwk('...', {...}); - */ -module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { - // Process parameters - if (typeof extraKeys === 'string') { - type = extraKeys; - extraKeys = {}; - } - - // Unpack the PEM - const key = rsaPemUnpack(pemKey); function base64Url(buffer) { return arrayBufferToBase64(buffer) From ae2dbc0c807a3082bdccfaaf6b8d05c9a0a95cd2 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:04:40 +0300 Subject: [PATCH 12/18] More versatile key type check --- rsa-pem-to-jwk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 9dcf6a9..8e176a3 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -55,7 +55,7 @@ module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { // Check requested JWK and given PEM types keyType = keyType.toLowerCase(); - if (type !== keyType) { + if (type === 'private' && keyType === 'public') { throw Error(`RSA type mismatch: requested ${type}, given ${keyType}.`); } type = keyType; From d8053e933a91773ebe1ed08472821c51a3d17ed0 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:05:59 +0300 Subject: [PATCH 13/18] Key type check fix --- rsa-pem-to-jwk.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 8e176a3..7cca551 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -55,10 +55,11 @@ module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { // Check requested JWK and given PEM types keyType = keyType.toLowerCase(); - if (type === 'private' && keyType === 'public') { + if (!type) { + type = keyType; + } else if (type === 'private' && keyType === 'public') { throw Error(`RSA type mismatch: requested ${type}, given ${keyType}.`); } - type = keyType; // PEM base64 to ArrayBuffer const derKey = new Uint8Array(base64toArrayBuffer(pemKey.join(''))); From 7dd0ce23eb3697aff7164de5c84d10e9a91bbaa9 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:11:42 +0300 Subject: [PATCH 14/18] Disabled exceptions --- rsa-pem-to-jwk.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 7cca551..11e1998 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -50,7 +50,8 @@ module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { // Check and remove RSA key header/footer let keyType = (/-----BEGIN RSA (PRIVATE|PUBLIC) KEY-----/.exec(pemKey.shift()) || [])[1]; if (!keyType || !RegExp(`-----END RSA ${keyType} KEY-----`).exec(pemKey.pop())) { - throw Error('Headers not supported.'); + //throw Error('Headers not supported.'); + return; } // Check requested JWK and given PEM types @@ -58,7 +59,8 @@ module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { if (!type) { type = keyType; } else if (type === 'private' && keyType === 'public') { - throw Error(`RSA type mismatch: requested ${type}, given ${keyType}.`); + //throw Error(`RSA type mismatch: requested ${type}, given ${keyType}.`); + return; } // PEM base64 to ArrayBuffer From b5524a84582e2f3c861b3d2d2f22dd9864a3ac9d Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:13:16 +0300 Subject: [PATCH 15/18] 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1c6ebd5..58f2676 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsa-pem-to-jwk", - "version": "1.1.3", + "version": "2.0.0", "description": "Converts PEM encoded RSA public and private keys to the JWK (JSON Web Key) format.", "main": "rsa-pem-to-jwk.js", "scripts": { From e7b55fab43ad81ad274bd9b55fdd4019301570a0 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:13:37 +0300 Subject: [PATCH 16/18] Changed author --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 58f2676..3d275e6 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "jwk", "jwks" ], - "author": "Andrew Balmos (https://github.com/abalmos)", + "author": "xxxzsx (https://github.com/xxxzsx)", "license": "Apache 2.0", "bugs": { "url": "https://github.com/OADA/rsa-pem-to-jwk/issues" From a1afc85e16c7bc12cb1cfb22bd15bc1b77316d8c Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:20:08 +0300 Subject: [PATCH 17/18] PKCS#8 headers support --- rsa-pem-to-jwk.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 11e1998..53cc84f 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -48,8 +48,8 @@ module.exports = function rsaPemToJwk(pemKey, extraKeys, type) { pemKey = String(pemKey).trim().split("\n"); // Check and remove RSA key header/footer - let keyType = (/-----BEGIN RSA (PRIVATE|PUBLIC) KEY-----/.exec(pemKey.shift()) || [])[1]; - if (!keyType || !RegExp(`-----END RSA ${keyType} KEY-----`).exec(pemKey.pop())) { + let keyType = (/-----BEGIN(?: RSA)? (PRIVATE|PUBLIC) KEY-----/.exec(pemKey.shift()) || [])[1]; + if (!keyType || !RegExp(`-----END( RSA)? ${keyType} KEY-----`).exec(pemKey.pop())) { //throw Error('Headers not supported.'); return; } From e016fd3013964dcaa77b496816e00cb9ade090b5 Mon Sep 17 00:00:00 2001 From: xxxzsx Date: Fri, 5 Mar 2021 07:44:59 +0300 Subject: [PATCH 18/18] 2.1.0 --- package.json | 2 +- rsa-pem-to-jwk.js | 2 +- test/rsa-pem-to-jwk.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3d275e6..3a75e89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsa-pem-to-jwk", - "version": "2.0.0", + "version": "2.1.0", "description": "Converts PEM encoded RSA public and private keys to the JWK (JSON Web Key) format.", "main": "rsa-pem-to-jwk.js", "scripts": { diff --git a/rsa-pem-to-jwk.js b/rsa-pem-to-jwk.js index 53cc84f..f828b34 100644 --- a/rsa-pem-to-jwk.js +++ b/rsa-pem-to-jwk.js @@ -1,4 +1,4 @@ -/* Copyright 2014 Open Ag Data Alliance +/* Copyright 2021 Open Ag Data Alliance * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/test/rsa-pem-to-jwk.test.js b/test/rsa-pem-to-jwk.test.js index 05c02ef..5c19238 100644 --- a/test/rsa-pem-to-jwk.test.js +++ b/test/rsa-pem-to-jwk.test.js @@ -1,5 +1,5 @@ /* - * Copyright 2014 Open Ag Data Alliance + * Copyright 2021 Open Ag Data Alliance * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.