{"version":3,"file":"Base64Encode.js","sources":["../../src/encode/Base64Encode.ts"],"sourcesContent":["/*\r\n * Copyright (c) Microsoft Corporation. All rights reserved.\r\n * Licensed under the MIT License.\r\n */\r\n\r\nimport { Constants } from \"@azure/msal-common\";\r\nimport { BrowserStringUtils } from \"../utils/BrowserStringUtils\";\r\n\r\n/**\r\n * Class which exposes APIs to encode plaintext to base64 encoded string. See here for implementation details:\r\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\r\n */\r\nexport class Base64Encode {\r\n \r\n /**\r\n * Returns URL Safe b64 encoded string from a plaintext string.\r\n * @param input \r\n */\r\n urlEncode(input: string): string {\r\n return encodeURIComponent(this.encode(input)\r\n .replace(/=/g, Constants.EMPTY_STRING)\r\n .replace(/\\+/g, \"-\")\r\n .replace(/\\//g, \"_\"));\r\n }\r\n\r\n /**\r\n * Returns URL Safe b64 encoded string from an int8Array.\r\n * @param inputArr \r\n */\r\n urlEncodeArr(inputArr: Uint8Array): string {\r\n return this.base64EncArr(inputArr)\r\n .replace(/=/g, Constants.EMPTY_STRING)\r\n .replace(/\\+/g, \"-\")\r\n .replace(/\\//g, \"_\");\r\n }\r\n\r\n /**\r\n * Returns b64 encoded string from plaintext string.\r\n * @param input \r\n */\r\n encode(input: string): string {\r\n const inputUtf8Arr = BrowserStringUtils.stringToUtf8Arr(input);\r\n return this.base64EncArr(inputUtf8Arr);\r\n }\r\n\r\n /**\r\n * Base64 encode byte array\r\n * @param aBytes \r\n */\r\n private base64EncArr(aBytes: Uint8Array): string { \r\n const eqLen = (3 - (aBytes.length % 3)) % 3;\r\n let sB64Enc = Constants.EMPTY_STRING;\r\n \r\n for (let nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {\r\n nMod3 = nIdx % 3;\r\n /* Uncomment the following line in order to split the output in lines 76-character long: */\r\n /*\r\n *if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += \"\\r\\n\"; }\r\n */\r\n nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);\r\n if (nMod3 === 2 || aBytes.length - nIdx === 1) {\r\n sB64Enc += String.fromCharCode(\r\n this.uint6ToB64(nUint24 >>> 18 & 63), \r\n this.uint6ToB64(nUint24 >>> 12 & 63), \r\n this.uint6ToB64(nUint24 >>> 6 & 63), \r\n this.uint6ToB64(nUint24 & 63)\r\n );\r\n nUint24 = 0;\r\n }\r\n }\r\n\r\n return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? \"=\" : \"==\");\r\n }\r\n\r\n /**\r\n * Base64 string to array encoding helper\r\n * @param nUint6 \r\n */\r\n private uint6ToB64 (nUint6: number): number {\r\n return nUint6 < 26 ?\r\n nUint6 + 65\r\n : nUint6 < 52 ?\r\n nUint6 + 71\r\n : nUint6 < 62 ?\r\n nUint6 - 4\r\n : nUint6 === 62 ?\r\n 43\r\n : nUint6 === 63 ?\r\n 47\r\n :\r\n 65;\r\n }\r\n}\r\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,GAA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