Skip to content

Instantly share code, notes, and snippets.

@ernestohs
Last active December 14, 2015 06:09
Show Gist options
  • Save ernestohs/5040144 to your computer and use it in GitHub Desktop.
Save ernestohs/5040144 to your computer and use it in GitHub Desktop.
Javascript Base64 encode/decode
;"strict";
window.Base64 = {
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode: function (input) {
var output = "";
var chr = [];
var enc = [];
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr[0] = input.charCodeAt(i++);
chr[1] = input.charCodeAt(i++);
chr[2] = input.charCodeAt(i++);
enc[0] = chr[0] >> 2;
enc[1] = ((chr[0] & 3) << 4) | (chr[1] >> 4);
enc[2] = ((chr[1] & 15) << 2) | (chr[2] >> 6);
enc[3] = chr[2] & 63;
if (isNaN(chr[1])) {
enc[2] = enc[3] = 64;
} else if (isNaN(chr[2])) {
enc[3] = 64;
}
output = output + this._keyStr.charAt(enc[0]) + this._keyStr.charAt(enc[1]) + this._keyStr.charAt(enc[2]) + this._keyStr.charAt(enc[3]);
}
return output;
},
// public method for decoding
decode: function (input) {
var output = "";
var chr = [];
var enc = [];
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc[0] = this._keyStr.indexOf(input.charAt(i++));
enc[1] = this._keyStr.indexOf(input.charAt(i++));
enc[2] = this._keyStr.indexOf(input.charAt(i++));
enc[3] = this._keyStr.indexOf(input.charAt(i++));
chr[0] = (enc[0] << 2) | (enc[1] >> 4);
chr[1] = ((enc[1] & 15) << 4) | (enc[2] >> 2);
chr[2] = ((enc[2] & 3) << 6) | enc[3];
output = output + String.fromCharCode(chr[0]);
if (enc[2] != 64) {
output = output + String.fromCharCode(chr[1]);
}
if (enc[3] != 64) {
output = output + String.fromCharCode(chr[2]);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode: function (string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode: function (utftext) {
var string = "";
var i = 0;
var c = 0;
var c1 = 0;
var c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
};
@ernestohs
Copy link
Author

Basic tests

var stringTestValue = 'Text test';

console.assert(Base64.decode(Base64.encode(stringTestValue)) === stringTestValue);
console.assert(Base64.encode('Alpha') === "QWxwaGE=");
console.assert(Base64.encode('') === '' );
console.assert(Base64.encode(' ')  === "IA==");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment