3 var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6 // public method for encoding
7 exports.encode = function(input, utf8) {
9 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
12 while (i < input.length) {
14 chr1 = input.charCodeAt(i++);
15 chr2 = input.charCodeAt(i++);
16 chr3 = input.charCodeAt(i++);
19 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
20 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
26 else if (isNaN(chr3)) {
30 output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
37 // public method for decoding
38 exports.decode = function(input, utf8) {
41 var enc1, enc2, enc3, enc4;
44 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
46 while (i < input.length) {
48 enc1 = _keyStr.indexOf(input.charAt(i++));
49 enc2 = _keyStr.indexOf(input.charAt(i++));
50 enc3 = _keyStr.indexOf(input.charAt(i++));
51 enc4 = _keyStr.indexOf(input.charAt(i++));
53 chr1 = (enc1 << 2) | (enc2 >> 4);
54 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
55 chr3 = ((enc3 & 3) << 6) | enc4;
57 output = output + String.fromCharCode(chr1);
60 output = output + String.fromCharCode(chr2);
63 output = output + String.fromCharCode(chr3);