Home > Javascript > Base64 Conversion in JavaScript

Base64 Conversion in JavaScript


Note that the data should not contain any unicode characters. In other words, the data must be
a string that each character of this string is representing a single byte. Therefor, if you want to encode strings
that contains unicode characters you should first convert it to UTF-8. (see UTF-8 in JS)

function base64Encode(data){
 if (typeof(btoa) == ‘function’) return btoa(data);//use internal base64 functions if available (gecko only)
 var b64_map = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=’;
 var byte1, byte2, byte3;
 var ch1, ch2, ch3, ch4;
 var result = new Array(); //array is used instead of string because in most of browsers working with large arrays is faster than working with large strings
 var j=0;
 for (var i=0; i<data.length; i+=3) {
  byte1 = data.charCodeAt(i);
  byte2 = data.charCodeAt(i+1);
  byte3 = data.charCodeAt(i+2);
  ch1 = byte1 >> 2;
  ch2 = ((byte1 & 3) << 4) | (byte2 >> 4);
  ch3 = ((byte2 & 15) << 2) | (byte3 >> 6);
  ch4 = byte3 & 63;  
  if (isNaN(byte2)) {
   ch3 = ch4 = 64;
  } else if (isNaN(byte3)) {
   ch4 = 64;
  }
  result[j++] = b64_map.charAt(ch1)+b64_map.charAt(ch2)+b64_map.charAt(ch3)+b64_map.charAt(ch4);
 }
 return result.join(”);
}

//Decodes Base64 formated data
function base64Decode(data){
 data = data.replace(/[^a-z0-9\+\/=]/ig, ”);// strip none base64 characters
 if (typeof(atob) == ‘function’) return atob(data);//use internal base64 functions if available (gecko only)
 var b64_map = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=’;
 var byte1, byte2, byte3;
 var ch1, ch2, ch3, ch4;
 var result = new Array(); //array is used instead of string because in most of browsers working with large arrays is faster than working with large strings
 var j=0;
 while ((data.length%4) != 0) {
  data += ‘=’;
 } 
 for (var i=0; i<data.length; i+=4) {
  ch1 = b64_map.indexOf(data.charAt(i));
  ch2 = b64_map.indexOf(data.charAt(i+1));
  ch3 = b64_map.indexOf(data.charAt(i+2));
  ch4 = b64_map.indexOf(data.charAt(i+3));
  byte1 = (ch1 << 2) | (ch2 >> 4);
  byte2 = ((ch2 & 15) << 4) | (ch3 >> 2);
  byte3 = ((ch3 & 3) << 6) | ch4;
  result[j++] = String.fromCharCode(byte1);
  if (ch3 != 64) result[j++] = String.fromCharCode(byte2);
  if (ch4 != 64) result[j++] = String.fromCharCode(byte3); 
 }

 return result.join(”);
}

Advertisements
Categories: Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: