manman89

JAVASCRIPT - BASE64

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