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 keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
8 * Encodes a string in base64
9 * @param {String} input The string to encode in base64.
11 function encode64(input) {
14 var enc1, enc2, enc3, enc4;
18 chr1 = input.charCodeAt(i++);
19 chr2 = input.charCodeAt(i++);
20 chr3 = input.charCodeAt(i++);
23 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
24 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
29 } else if (isNaN(chr3)) {
33 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
34 keyStr.charAt(enc3) + keyStr.charAt(enc4);
35 } while (i < input.length);
41 * Decodes a base64 string.
42 * @param {String} input The string to decode.
44 function decode64(input) {
47 var enc1, enc2, enc3, enc4;
50 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
51 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
54 enc1 = keyStr.indexOf(input.charAt(i++));
55 enc2 = keyStr.indexOf(input.charAt(i++));
56 enc3 = keyStr.indexOf(input.charAt(i++));
57 enc4 = keyStr.indexOf(input.charAt(i++));
59 chr1 = (enc1 << 2) | (enc2 >> 4);
60 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
61 chr3 = ((enc3 & 3) << 6) | enc4;
63 output = output + String.fromCharCode(chr1);
66 output = output + String.fromCharCode(chr2);
69 output = output + String.fromCharCode(chr3);
71 } while (i < input.length);