1 // This code was written by Tyler Akins and has been placed in the
2 // public domain. It would be nice if you left this header intact.
3 // Base64 code from Tyler Akins -- http://rumkin.com
5 var Base64 = (function () {
6 var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
10 * Encodes a string in base64
11 * @param {String} input The string to encode in base64.
13 encode: function (input) {
16 var enc1, enc2, enc3, enc4;
20 chr1 = input.charCodeAt(i++);
21 chr2 = input.charCodeAt(i++);
22 chr3 = input.charCodeAt(i++);
25 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
26 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
31 } else if (isNaN(chr3)) {
35 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
36 keyStr.charAt(enc3) + keyStr.charAt(enc4);
37 } while (i < input.length);
43 * Decodes a base64 string.
44 * @param {String} input The string to decode.
46 decode: function (input) {
49 var enc1, enc2, enc3, enc4;
52 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
53 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
56 enc1 = keyStr.indexOf(input.charAt(i++));
57 enc2 = keyStr.indexOf(input.charAt(i++));
58 enc3 = keyStr.indexOf(input.charAt(i++));
59 enc4 = keyStr.indexOf(input.charAt(i++));
61 chr1 = (enc1 << 2) | (enc2 >> 4);
62 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
63 chr3 = ((enc3 & 3) << 6) | enc4;
65 output = output + String.fromCharCode(chr1);
68 output = output + String.fromCharCode(chr2);
71 output = output + String.fromCharCode(chr3);
73 } while (i < input.length);