{"version":3,"file":"Base64Encode.js","sources":["../../src/encode/Base64Encode.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants } from \"@azure/msal-common\";\nimport { BrowserStringUtils } from \"../utils/BrowserStringUtils\";\n\n/**\n * Class which exposes APIs to encode plaintext to base64 encoded string. See here for implementation details:\n * https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_2_%E2%80%93_JavaScript's_UTF-16_%3E_UTF-8_%3E_base64\n */\nexport class Base64Encode {\n \n /**\n * Returns URL Safe b64 encoded string from a plaintext string.\n * @param input \n */\n urlEncode(input: string): string {\n return encodeURIComponent(this.encode(input)\n .replace(/=/g, Constants.EMPTY_STRING)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\"));\n }\n\n /**\n * Returns URL Safe b64 encoded string from an int8Array.\n * @param inputArr \n */\n urlEncodeArr(inputArr: Uint8Array): string {\n return this.base64EncArr(inputArr)\n .replace(/=/g, Constants.EMPTY_STRING)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\");\n }\n\n /**\n * Returns b64 encoded string from plaintext string.\n * @param input \n */\n encode(input: string): string {\n const inputUtf8Arr = BrowserStringUtils.stringToUtf8Arr(input);\n return this.base64EncArr(inputUtf8Arr);\n }\n\n /**\n * Base64 encode byte array\n * @param aBytes \n */\n private base64EncArr(aBytes: Uint8Array): string { \n const eqLen = (3 - (aBytes.length % 3)) % 3;\n let sB64Enc = Constants.EMPTY_STRING;\n \n for (let nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {\n nMod3 = nIdx % 3;\n /* Uncomment the following line in order to split the output in lines 76-character long: */\n /*\n *if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += \"\\r\\n\"; }\n */\n nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);\n if (nMod3 === 2 || aBytes.length - nIdx === 1) {\n sB64Enc += String.fromCharCode(\n this.uint6ToB64(nUint24 >>> 18 & 63), \n this.uint6ToB64(nUint24 >>> 12 & 63), \n this.uint6ToB64(nUint24 >>> 6 & 63), \n this.uint6ToB64(nUint24 & 63)\n );\n nUint24 = 0;\n }\n }\n\n return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? \"=\" : \"==\");\n }\n\n /**\n * Base64 string to array encoding helper\n * @param nUint6 \n */\n private uint6ToB64 (nUint6: number): number {\n return nUint6 < 26 ?\n nUint6 + 65\n : nUint6 < 52 ?\n nUint6 + 71\n : nUint6 < 62 ?\n nUint6 - 4\n : nUint6 === 62 ?\n 43\n : nUint6 === 63 ?\n 47\n :\n 65;\n }\n}\n"],"names":[],"mappings":";;;;;AAAA;;;;AAQA;;;;;IAIA;KAgFC;;;;;IA1EG,gCAAS,GAAT,UAAU,KAAa;QACnB,OAAO,kBAAkB,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;aACvC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,YAAY,CAAC;aACrC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;KAC7B;;;;;IAMD,mCAAY,GAAZ,UAAa,QAAoB;QAC7B,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC;aAC7B,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,YAAY,CAAC;aACrC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KAC5B;;;;;IAMD,6BAAM,GAAN,UAAO,KAAa;QAChB,IAAM,YAAY,GAAG,kBAAkB,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAC/D,OAAO,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;KAC1C;;;;;IAMO,mCAAY,GAApB,UAAqB,MAA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