{"ScriptPreparationCode":"const charset = \u0027ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\u002B/\u0027;\r\nconst paddingChar = \u0027=\u0027;\r\nfunction encode(input) {\r\n const paddings = 3 - (input.length % 3 || 3);\r\n let encoded = \u0027\u0027;\r\n let binaries = \u0027\u0027;\r\n for (let i = 0; i \u003C input.length \u002B paddings; i\u002B\u002B) {\r\n const char = input[i];\r\n const binary = char ? char.charCodeAt(0).toString(2).padStart(8, \u00270\u0027) : \u002700000000\u0027;\r\n // the binaries length could only be 0, 2 or 4 because the bits will be consumed once become 6\r\n switch (binaries.length) {\r\n case 0:\r\n encoded \u002B= charset[parseInt(binary.slice(0, 6), 2)];\r\n binaries = binary.slice(6);\r\n break;\r\n case 2:\r\n encoded \u002B= charset[parseInt(\u0060${binaries}${binary.slice(0, 4)}\u0060, 2)];\r\n binaries = binary.slice(4);\r\n break;\r\n case 4:\r\n // the paddings count could only be 0, 1 or 2\r\n switch (paddings) {\r\n case 0:\r\n encoded \u002B= charset[parseInt(\u0060${binaries}${binary.slice(0, 2)}\u0060, 2)];\r\n encoded \u002B= charset[parseInt(binary.slice(2), 2)];\r\n break;\r\n case 1:\r\n // when there\u0027s only one padding and current char is undedefined (exceeded input string ranged)\r\n // which means the last char is a padding char\r\n if (!char) {\r\n encoded \u002B= charset[parseInt(\u0060${binaries}${binary.slice(0, 2)}\u0060, 2)];\r\n encoded \u002B= paddingChar;\r\n }\r\n else {\r\n encoded \u002B= charset[parseInt(\u0060${binaries}${binary.slice(0, 2)}\u0060, 2)];\r\n encoded \u002B= charset[parseInt(binary.slice(2), 2)];\r\n }\r\n break;\r\n case 2:\r\n // when there are 2 paddings and previous char is undedefined (exceeded input string ranged)\r\n // which means the last 2 chars are padding chars\r\n if (!input[i - 1]) {\r\n encoded \u002B= paddingChar;\r\n encoded \u002B= paddingChar;\r\n }\r\n else {\r\n encoded \u002B= charset[parseInt(\u0060${binaries}${binary.slice(0, 2)}\u0060, 2)];\r\n encoded \u002B= charset[parseInt(binary.slice(2), 2)];\r\n }\r\n break;\r\n }\r\n binaries = \u0027\u0027;\r\n break;\r\n }\r\n }\r\n return encoded;\r\n}\r\nfunction decode(input) {\r\n let decoded = \u0027\u0027;\r\n let binaries = \u0027\u0027;\r\n for (let i = 0; i \u003C input.length; i\u002B\u002B) {\r\n const char = input[i];\r\n // once reached the padding char, don\u0027t need to continue the loop\r\n if (char === paddingChar) {\r\n break;\r\n }\r\n const code = charset.indexOf(char);\r\n const binary = code.toString(2).padStart(6, \u00270\u0027);\r\n // binaries length could only be 0, 2, 4 or 6 because the bits will be consumed once become 8\r\n switch (binaries.length) {\r\n case 0:\r\n binaries \u002B= binary;\r\n break;\r\n case 2:\r\n decoded \u002B= String.fromCharCode(parseInt(\u0060${binaries}${binary}\u0060, 2));\r\n binaries = \u0027\u0027;\r\n break;\r\n case 4:\r\n decoded \u002B= String.fromCharCode(parseInt(\u0060${binaries}${binary.slice(0, 4)}\u0060, 2));\r\n binaries = binary.slice(4);\r\n break;\r\n case 6:\r\n decoded \u002B= String.fromCharCode(parseInt(\u0060${binaries}${binary.slice(0, 2)}\u0060, 2));\r\n binaries = binary.slice(2);\r\n break;\r\n }\r\n }\r\n return decoded;\r\n}\r\n\r\nvar base64 = {\r\n encode,\r\n decode,\r\n};\r\n\r\n/**\r\n*\r\n* Base64 encode / decode\r\n* http://www.webtoolkit.info/\r\n*\r\n**/\r\nvar Base64 = {\r\n\r\n // private property\r\n _keyStr : \u0022ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\u002B/=\u0022,\r\n\r\n // public method for encoding\r\n encode : function (input) {\r\n var output = \u0022\u0022;\r\n var chr1, chr2, chr3, enc1, enc2, enc3, enc4;\r\n var i = 0;\r\n\r\n input = Base64._utf8_encode(input);\r\n\r\n while (i \u003C input.length) {\r\n\r\n chr1 = input.charCodeAt(i\u002B\u002B);\r\n chr2 = input.charCodeAt(i\u002B\u002B);\r\n chr3 = input.charCodeAt(i\u002B\u002B);\r\n\r\n enc1 = chr1 \u003E\u003E 2;\r\n enc2 = ((chr1 \u0026 3) \u003C\u003C 4) | (chr2 \u003E\u003E 4);\r\n enc3 = ((chr2 \u0026 15) \u003C\u003C 2) | (chr3 \u003E\u003E 6);\r\n enc4 = chr3 \u0026 63;\r\n\r\n if (isNaN(chr2)) {\r\n enc3 = enc4 = 64;\r\n } else if (isNaN(chr3)) {\r\n enc4 = 64;\r\n }\r\n\r\n output = output \u002B\r\n this._keyStr.charAt(enc1) \u002B this._keyStr.charAt(enc2) \u002B\r\n this._keyStr.charAt(enc3) \u002B this._keyStr.charAt(enc4);\r\n }\r\n return output;\r\n },\r\n\r\n // public method for decoding\r\n decode : function (input) {\r\n var output = \u0022\u0022;\r\n var chr1, chr2, chr3;\r\n var enc1, enc2, enc3, enc4;\r\n var i = 0;\r\n\r\n input = input.replace(/[^A-Za-z0-9\\\u002B\\/\\=]/g, \u0022\u0022);\r\n\r\n while (i \u003C input.length) {\r\n\r\n enc1 = this._keyStr.indexOf(input.charAt(i\u002B\u002B));\r\n enc2 = this._keyStr.indexOf(input.charAt(i\u002B\u002B));\r\n enc3 = this._keyStr.indexOf(input.charAt(i\u002B\u002B));\r\n enc4 = this._keyStr.indexOf(input.charAt(i\u002B\u002B));\r\n\r\n chr1 = (enc1 \u003C\u003C 2) | (enc2 \u003E\u003E 4);\r\n chr2 = ((enc2 \u0026 15) \u003C\u003C 4) | (enc3 \u003E\u003E 2);\r\n chr3 = ((enc3 \u0026 3) \u003C\u003C 6) | enc4;\r\n\r\n output = output \u002B String.fromCharCode(chr1);\r\n\r\n if (enc3 != 64) {\r\n output = output \u002B String.fromCharCode(chr2);\r\n }\r\n if (enc4 != 64) {\r\n output = output \u002B String.fromCharCode(chr3);\r\n }\r\n }\r\n\r\n output = Base64._utf8_decode(output);\r\n\r\n return output;\r\n },\r\n\r\n // private method for UTF-8 encoding\r\n _utf8_encode : function (string) {\r\n string = string.replace(/\\r\\n/g,\u0022\\n\u0022);\r\n var utftext = \u0022\u0022;\r\n\r\n for (var n = 0; n \u003C string.length; n\u002B\u002B) {\r\n\r\n var c = string.charCodeAt(n);\r\n\r\n if (c \u003C 128) {\r\n utftext \u002B= String.fromCharCode(c);\r\n }\r\n else if((c \u003E 127) \u0026\u0026 (c \u003C 2048)) {\r\n utftext \u002B= String.fromCharCode((c \u003E\u003E 6) | 192);\r\n utftext \u002B= String.fromCharCode((c \u0026 63) | 128);\r\n }\r\n else {\r\n utftext \u002B= String.fromCharCode((c \u003E\u003E 12) | 224);\r\n utftext \u002B= String.fromCharCode(((c \u003E\u003E 6) \u0026 63) | 128);\r\n utftext \u002B= String.fromCharCode((c \u0026 63) | 128);\r\n }\r\n }\r\n return utftext;\r\n },\r\n\r\n // private method for UTF-8 decoding\r\n _utf8_decode : function (utftext) {\r\n var string = \u0022\u0022;\r\n var i = 0;\r\n var c = c1 = c2 = 0;\r\n\r\n while ( i \u003C utftext.length ) {\r\n\r\n c = utftext.charCodeAt(i);\r\n\r\n if (c \u003C 128) {\r\n string \u002B= String.fromCharCode(c);\r\n i\u002B\u002B;\r\n }\r\n else if((c \u003E 191) \u0026\u0026 (c \u003C 224)) {\r\n c2 = utftext.charCodeAt(i\u002B1);\r\n string \u002B= String.fromCharCode(((c \u0026 31) \u003C\u003C 6) | (c2 \u0026 63));\r\n i \u002B= 2;\r\n }\r\n else {\r\n c2 = utftext.charCodeAt(i\u002B1);\r\n c3 = utftext.charCodeAt(i\u002B2);\r\n string \u002B= String.fromCharCode(((c \u0026 15) \u003C\u003C 12) | ((c2 \u0026 63) \u003C\u003C 6) | (c3 \u0026 63));\r\n i \u002B= 3;\r\n }\r\n }\r\n return string;\r\n }\r\n}\r\n\r\nvar str = \u0027PYtFLbTaRSeG9qH781zL0d6irQoPYJDFT6ubjfXIAKfQEOXC\u0027;","TestCases":[{"Name":"Base64 1","Code":"Base64.decode(Base64.encode(str))","IsDeferred":false},{"Name":"base64 2","Code":"base64.decode(base64.encode(str))","IsDeferred":false}]}