|
| 1 | +/** |
| 2 | + * |
| 3 | + * Base64 encode / decode |
| 4 | + * http://www.webtoolkit.info/javascript_base64.html |
| 5 | + * |
| 6 | + **/ |
| 7 | +export const Base64 = { |
| 8 | + // private property |
| 9 | + _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", |
| 10 | + |
| 11 | + // public method for encoding |
| 12 | + encode: function (input: string) { |
| 13 | + let output = ""; |
| 14 | + let chr1, chr2, chr3, enc1, enc2, enc3, enc4; |
| 15 | + let i = 0; |
| 16 | + |
| 17 | + input = Base64._utf8_encode(input); |
| 18 | + |
| 19 | + while (i < input.length) { |
| 20 | + chr1 = input.charCodeAt(i++); |
| 21 | + chr2 = input.charCodeAt(i++); |
| 22 | + chr3 = input.charCodeAt(i++); |
| 23 | + |
| 24 | + enc1 = chr1 >> 2; |
| 25 | + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
| 26 | + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
| 27 | + enc4 = chr3 & 63; |
| 28 | + |
| 29 | + if (isNaN(chr2)) { |
| 30 | + enc3 = enc4 = 64; |
| 31 | + } else if (isNaN(chr3)) { |
| 32 | + enc4 = 64; |
| 33 | + } |
| 34 | + |
| 35 | + output = |
| 36 | + output + |
| 37 | + this._keyStr.charAt(enc1) + |
| 38 | + this._keyStr.charAt(enc2) + |
| 39 | + this._keyStr.charAt(enc3) + |
| 40 | + this._keyStr.charAt(enc4); |
| 41 | + } // Whend |
| 42 | + |
| 43 | + return output; |
| 44 | + }, // End Function encode |
| 45 | + |
| 46 | + // public method for decoding |
| 47 | + decode: function (input: string) { |
| 48 | + let output = ""; |
| 49 | + let chr1, chr2, chr3; |
| 50 | + let enc1, enc2, enc3, enc4; |
| 51 | + let i = 0; |
| 52 | + |
| 53 | + input = input.replace(/[^A-Za-z0-9+/=]/g, ""); |
| 54 | + while (i < input.length) { |
| 55 | + enc1 = this._keyStr.indexOf(input.charAt(i++)); |
| 56 | + enc2 = this._keyStr.indexOf(input.charAt(i++)); |
| 57 | + enc3 = this._keyStr.indexOf(input.charAt(i++)); |
| 58 | + enc4 = this._keyStr.indexOf(input.charAt(i++)); |
| 59 | + |
| 60 | + chr1 = (enc1 << 2) | (enc2 >> 4); |
| 61 | + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
| 62 | + chr3 = ((enc3 & 3) << 6) | enc4; |
| 63 | + |
| 64 | + output = output + String.fromCharCode(chr1); |
| 65 | + |
| 66 | + if (enc3 != 64) { |
| 67 | + output = output + String.fromCharCode(chr2); |
| 68 | + } |
| 69 | + |
| 70 | + if (enc4 != 64) { |
| 71 | + output = output + String.fromCharCode(chr3); |
| 72 | + } |
| 73 | + } // Whend |
| 74 | + |
| 75 | + output = Base64._utf8_decode(output); |
| 76 | + |
| 77 | + return output; |
| 78 | + }, // End Function decode |
| 79 | + |
| 80 | + // private method for UTF-8 encoding |
| 81 | + _utf8_encode: function (string: string) { |
| 82 | + let utftext = ""; |
| 83 | + string = string.replace(/\r\n/g, "\n"); |
| 84 | + |
| 85 | + for (let n = 0; n < string.length; n++) { |
| 86 | + const c = string.charCodeAt(n); |
| 87 | + |
| 88 | + if (c < 128) { |
| 89 | + utftext += String.fromCharCode(c); |
| 90 | + } else if (c > 127 && c < 2048) { |
| 91 | + utftext += String.fromCharCode((c >> 6) | 192); |
| 92 | + utftext += String.fromCharCode((c & 63) | 128); |
| 93 | + } else { |
| 94 | + utftext += String.fromCharCode((c >> 12) | 224); |
| 95 | + utftext += String.fromCharCode(((c >> 6) & 63) | 128); |
| 96 | + utftext += String.fromCharCode((c & 63) | 128); |
| 97 | + } |
| 98 | + } // Next n |
| 99 | + |
| 100 | + return utftext; |
| 101 | + }, // End Function _utf8_encode |
| 102 | + |
| 103 | + // private method for UTF-8 decoding |
| 104 | + _utf8_decode: function (utftext: string) { |
| 105 | + let string = ""; |
| 106 | + let i = 0; |
| 107 | + let c, c1, c2, c3; |
| 108 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 109 | + c = c1 = c2 = 0; |
| 110 | + |
| 111 | + while (i < utftext.length) { |
| 112 | + c = utftext.charCodeAt(i); |
| 113 | + |
| 114 | + if (c < 128) { |
| 115 | + string += String.fromCharCode(c); |
| 116 | + i++; |
| 117 | + } else if (c > 191 && c < 224) { |
| 118 | + c2 = utftext.charCodeAt(i + 1); |
| 119 | + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); |
| 120 | + i += 2; |
| 121 | + } else { |
| 122 | + c2 = utftext.charCodeAt(i + 1); |
| 123 | + c3 = utftext.charCodeAt(i + 2); |
| 124 | + string += String.fromCharCode( |
| 125 | + ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63) |
| 126 | + ); |
| 127 | + i += 3; |
| 128 | + } |
| 129 | + } // Whend |
| 130 | + |
| 131 | + return string; |
| 132 | + }, // End Function _utf8_decode |
| 133 | +}; |
0 commit comments