| 1 package freecs.util; 2 3 import java.util.NoSuchElementException ; 4 import java.util.StringTokenizer ; 5 6 public class HttpAuth { 7 8 13 14 public String username; 15 public String password; 16 17 public static HttpAuth parse(String auth) { 18 19 if ((auth == null) || "".equals(auth)) { 20 return null; 21 } 22 23 StringTokenizer tok; 24 25 if (auth.startsWith("Basic ")) { 26 String decoded = new String (decode((auth.substring(6)).toCharArray())); 27 tok = new StringTokenizer (decoded ,":"); 28 } else { 29 String decoded = new String (decode(auth.toCharArray())); 30 tok = new StringTokenizer (decoded, ":"); 31 } 32 33 HttpAuth reval = new HttpAuth(); 34 try { 35 reval.username = tok.nextToken(); 36 } catch (NoSuchElementException e) { 37 reval.username = null; 38 } 39 40 try { 41 reval.password = tok.nextToken(); 42 } catch (NoSuchElementException e) { 43 reval.password = null; 44 } 45 return reval; 46 47 } 48 49 50 51 52 53 54 80 81 88 public static final char[] encode(byte[] data) 89 { 90 char[] out = new char[((data.length + 2) / 3) * 4]; 91 92 for (int i=0, index=0; i<data.length; i+=3, index+=4) { 97 boolean quad = false; 98 boolean trip = false; 99 100 int val = (0xFF & (int) data[i]); 101 val <<= 8; 102 if ((i+1) < data.length) { 103 val |= (0xFF & (int) data[i+1]); 104 trip = true; 105 } 106 val <<= 8; 107 if ((i+2) < data.length) { 108 val |= (0xFF & (int) data[i+2]); 109 quad = true; 110 } 111 out[index+3] = alphabet[(quad? (val & 0x3F): 64)]; 112 val >>= 6; 113 out[index+2] = alphabet[(trip? (val & 0x3F): 64)]; 114 val >>= 6; 115 out[index+1] = alphabet[val & 0x3F]; 116 val >>= 6; 117 out[index+0] = alphabet[val & 0x3F]; 118 } 119 return out; 120 } 121 122 133 public static final byte[] decode(char[] data) 134 { 135 142 int tempLen = data.length; 143 for( int ix=0; ix<data.length; ix++ ) 144 { 145 if( (data[ix] > 255) || codes[ data[ix] ] < 0 ) 146 --tempLen; } 148 153 int len = (tempLen / 4) * 3; 154 if ((tempLen % 4) == 3) len += 2; 155 if ((tempLen % 4) == 2) len += 1; 156 157 byte[] out = new byte[len]; 158 159 160 161 int shift = 0; int accum = 0; int index = 0; 164 165 for (int ix=0; ix<data.length; ix++) 167 { 168 int value = (data[ix]>255)? -1: codes[ data[ix] ]; 169 170 if ( value >= 0 ) { 172 accum <<= 6; shift += 6; accum |= value; if ( shift >= 8 ) { 177 shift -= 8; out[index++] = (byte) ((accum >> shift) & 0xff); 180 } 181 } 182 } 189 190 if( index != out.length) 192 { 193 throw new Error ("Miscalculated data length (wrote " + index + " instead of " + out.length + ")"); 194 } 195 196 return out; 197 } 198 199 200 private static char[] alphabet = 204 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" 205 .toCharArray(); 206 207 private static byte[] codes = new byte[256]; 211 static { 212 for (int i=0; i<256; i++) codes[i] = -1; 213 for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A'); 214 for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a'); 215 for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0'); 216 codes['+'] = 62; 217 codes['/'] = 63; 218 } 219 220 } 221 | Popular Tags |