1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 1× 1× 1× 2× 10× 1× 10× 2× 2× 1× 2× | /* eslint no-bitwise: 0 */ function wrap(text: string, len: number) { 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; } // TODO: any function pemPrivateKey(key: any) { return `-----BEGIN PRIVATE KEY-----\n${wrap(key, 64)}-----END PRIVATE KEY-----`; } function stripPemFormatting(str: string) { return str .replace(/^-----BEGIN (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----$/m, "") .replace(/^-----END (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----$/m, "") .replace(/[\n\r]/g, ""); } // TODO: any export function arrayToPem(a: any[]) { return window.btoa(a.map(c => String.fromCharCode(c)).join("")); } function stringToArray(s: string) { // TODO: any return s.split("").map(c => (c as any).charCodeAt()); } export function pemToArray(pem: string) { 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, ]; export function pkcs1To8(privateKeyPkcs1Pem: string) { 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"] // ); |