Skip to content

Commit

Permalink
Trim PEM key
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Dec 9, 2020
1 parent c9836f1 commit 9821adf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/rsa/import_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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");
Expand Down
24 changes: 15 additions & 9 deletions tests/rsa/rsa.import_key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 9821adf

Please sign in to comment.