packages/ssoinabox-webui/root/usr/local/share/ssoinabox/htdocs/res/base64.js
changeset 4 2212b2ded8bf
equal deleted inserted replaced
3:a044870a9d3d 4:2212b2ded8bf
       
     1 var keyStr = "ABCDEFGHIJKLMNOP" +
       
     2        "QRSTUVWXYZabcdef" +
       
     3        "ghijklmnopqrstuv" +
       
     4        "wxyz0123456789+/" +
       
     5        "=";
       
     6        
       
     7 var Base64 = {
       
     8 
       
     9 	encode: function (input) {
       
    10 			input = escape(input);
       
    11 			var output = "";
       
    12 			var chr1, chr2, chr3 = "";
       
    13 			var enc1, enc2, enc3, enc4 = "";
       
    14 			var i = 0;
       
    15 			
       
    16 			do {
       
    17 				chr1 = input.charCodeAt(i++);
       
    18 				chr2 = input.charCodeAt(i++);
       
    19 				chr3 = input.charCodeAt(i++);
       
    20 				
       
    21 				enc1 = chr1 >> 2;
       
    22 				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
       
    23 				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
       
    24 				enc4 = chr3 & 63;
       
    25 				
       
    26 				if (isNaN(chr2)) {
       
    27 					enc3 = enc4 = 64;
       
    28 				} else if (isNaN(chr3)) {
       
    29 					enc4 = 64;
       
    30 				}
       
    31 				
       
    32 				output = output +
       
    33 				   keyStr.charAt(enc1) +
       
    34 				   keyStr.charAt(enc2) +
       
    35 				   keyStr.charAt(enc3) +
       
    36 				   keyStr.charAt(enc4);
       
    37 				chr1 = chr2 = chr3 = "";
       
    38 				enc1 = enc2 = enc3 = enc4 = "";
       
    39 			} while (i < input.length);
       
    40 			
       
    41 			return output;
       
    42 		}
       
    43 
       
    44 	, decode : function (input) {
       
    45 			var output = "";
       
    46 			var chr1, chr2, chr3 = "";
       
    47 			var enc1, enc2, enc3, enc4 = "";
       
    48 			var i = 0;
       
    49 			
       
    50 			// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
       
    51 			var base64test = /[^A-Za-z0-9\+\/\=]/g;
       
    52 			if (base64test.exec(input)) {
       
    53 			alert("There were invalid base64 characters in the input text.\n" +
       
    54 			      "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
       
    55 			      "Expect errors in decoding.");
       
    56 			}
       
    57 			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
       
    58 			
       
    59 			do {
       
    60 				enc1 = keyStr.indexOf(input.charAt(i++));
       
    61 				enc2 = keyStr.indexOf(input.charAt(i++));
       
    62 				enc3 = keyStr.indexOf(input.charAt(i++));
       
    63 				enc4 = keyStr.indexOf(input.charAt(i++));
       
    64 				
       
    65 				chr1 = (enc1 << 2) | (enc2 >> 4);
       
    66 				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
       
    67 				chr3 = ((enc3 & 3) << 6) | enc4;
       
    68 				
       
    69 				output = output + String.fromCharCode(chr1);
       
    70 				
       
    71 				if (enc3 != 64) {
       
    72 					output = output + String.fromCharCode(chr2);
       
    73 				}
       
    74 				if (enc4 != 64) {
       
    75 					output = output + String.fromCharCode(chr3);
       
    76 				}
       
    77 				
       
    78 				chr1 = chr2 = chr3 = "";
       
    79 				enc1 = enc2 = enc3 = enc4 = "";
       
    80 				
       
    81 			} while (i < input.length);
       
    82 			
       
    83 			return unescape(output);
       
    84 		}
       
    85 }