MCPcopy Create free account
hub / github.com/Moli-X/Resources / Base64

Function Base64

Script/TBPrice.js:305–402  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

303}
304
305function Base64() {
306 // private property
307 _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
308 // public method for encoding
309 this.encode = function (input) {
310 var output = "";
311 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
312 var i = 0;
313 input = _utf8_encode(input);
314 while (i < input.length) {
315 chr1 = input.charCodeAt(i++);
316 chr2 = input.charCodeAt(i++);
317 chr3 = input.charCodeAt(i++);
318 enc1 = chr1 >> 2;
319 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
320 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
321 enc4 = chr3 & 63;
322 if (isNaN(chr2)) {
323 enc3 = enc4 = 64;
324 } else if (isNaN(chr3)) {
325 enc4 = 64;
326 }
327 output = output +
328 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
329 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
330 }
331 return output;
332 }
333 // public method for decoding
334 this.decode = function (input) {
335 var output = "";
336 var chr1, chr2, chr3;
337 var enc1, enc2, enc3, enc4;
338 var i = 0;
339 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
340 while (i < input.length) {
341 enc1 = _keyStr.indexOf(input.charAt(i++));
342 enc2 = _keyStr.indexOf(input.charAt(i++));
343 enc3 = _keyStr.indexOf(input.charAt(i++));
344 enc4 = _keyStr.indexOf(input.charAt(i++));
345 chr1 = (enc1 << 2) | (enc2 >> 4);
346 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
347 chr3 = ((enc3 & 3) << 6) | enc4;
348 output = output + String.fromCharCode(chr1);
349 if (enc3 != 64) {
350 output = output + String.fromCharCode(chr2);
351 }
352 if (enc4 != 64) {
353 output = output + String.fromCharCode(chr3);
354 }
355 }
356 output = _utf8_decode(output);
357 return output;
358 }
359 // private method for UTF-8 encoding
360 _utf8_encode = function (string) {
361 string = string.replace(/\r\n/g, "\n");
362 var utftext = "";

Callers

nothing calls this directly

Calls 1

indexOfMethod · 0.45

Tested by

no test coverage detected