From 9821adf0e0f907c45481462a3f85e9e314af8456 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Wed, 9 Dec 2020 20:02:14 +0700 Subject: [PATCH] Trim PEM key --- src/rsa/import_key.ts | 3 ++- tests/rsa/rsa.import_key.test.ts | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index 10e60ab..7021fed 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -148,6 +148,7 @@ function rsa_import_pem_public(key: string): RSAKeyParams { */ function rsa_import_pem(key: string): RSAKeyParams { if (typeof key !== "string") throw new TypeError("PEM key must be string"); + const trimmedKey = key.trim(); const maps: [string, (key: string) => RSAKeyParams][] = [ ["-----BEGIN RSA PRIVATE KEY-----", rsa_import_pem_private], @@ -157,7 +158,7 @@ function rsa_import_pem(key: string): RSAKeyParams { ]; for (const [prefix, func] of maps) { - if (key.indexOf(prefix) === 0) return func(key); + if (trimmedKey.indexOf(prefix) === 0) return func(trimmedKey); } throw new TypeError("Unsupported key format"); diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index 6c6a6e8..e8d5915 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -80,14 +80,20 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => { -----END PRIVATE KEY-----`; const key = RSA.importKey(raw); - assertEquals(key.e, 65537n); - assertEquals( - key.n, - 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n, - ); - assertEquals( - key.d, - 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n, - ); + const n = + 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n; + + const e = 65537n; + const d = + 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n; + assertEquals(key.e, e); + assertEquals(key.n, n); + assertEquals(key.d, d); assertEquals(key.length, 256); + + // Testing key with trailing newline + const key2 = RSA.importKey(raw + "\n\r\n\r\n\r"); + assertEquals(key2.e, e); + assertEquals(key2.n, n); + assertEquals(key2.d, d); });