3 public abstract class SaslUtils
7 * a static array that maps 6 bit integers to a specific char
9 private final static char enc_table[] =
12 'A','B','C','D','E','F','G','H', // 0
13 'I','J','K','L','M','N','O','P', // 1
14 'Q','R','S','T','U','V','W','X', // 2
15 'Y','Z','a','b','c','d','e','f', // 3
16 'g','h','i','j','k','l','m','n', // 4
17 'o','p','q','r','s','t','u','v', // 5
18 'w','x','y','z','0','1','2','3', // 6
19 '4','5','6','7','8','9','+','/' // 7
21 private final static byte dec_table[] =
23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
24 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
26 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
27 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
28 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
29 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
30 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 * Base 64 Encodes a String in byte[] form
44 * @param buf String to be encoded
46 * @return the encoded value in string form
51 encode64( byte buf[] )
54 StringBuffer buffer = new StringBuffer();
58 for (int count = len / 3; count > 0; count--)
63 buffer.append(enc_table[(a >>> 2) & 0x3F]);
64 buffer.append(enc_table[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
65 buffer.append(enc_table[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
66 buffer.append(enc_table[c & 0x3F]);
68 /* if (i != 0 && i%57 == 0)
69 buffer.append("\r\n");*/
75 buffer.append(enc_table[(a >>> 2) & 0x3F]);
76 buffer.append(enc_table[((a << 4) & 0x30)]);
84 buffer.append(enc_table[(a >>> 2) & 0x3F]);
85 buffer.append(enc_table[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
86 buffer.append(enc_table[((b << 2) & 0x3c)]);
90 /*buffer.append("\r\n");*/
91 return buffer.toString();
95 * Base 64 Encodes a String in String form
97 * @param s String to be encoded
99 * @return the encoded value in string form
102 public static String encode64( String s )
104 return encode64( s.getBytes() );
109 * Base 64 Decodes a byte[] string
110 * Ignores trailing whitespace and newlines
112 * @param buf buffer to be decoded
114 * @return the decoded value in byte[] form
119 decode64( byte buf[] )
122 int i, len = buf.length;
125 for (i=len-1; i >= 0; --i)
134 // Hmm - should this be a "bad format MIME" exception instead?
136 throw new IllegalArgumentException("Length not a multiple of 4");
138 int ret_len = (real_len/4)*3 - padCount;
139 byte ret[] = new byte[ret_len];
142 byte[] t = new byte[4];
143 int output_index = 0;
145 t[0] = t[1] = t[2] = t[3] = 61; // Ascii =
154 output_index += decode64(ret, output_index, t[0], t[1], t[2], t[3]);
156 t[0] = t[1] = t[2] = t[3] = 61; // Ascii =
160 decode64(ret, output_index, t[0], t[1], t[2], t[3]);
166 * Base 64 Decodes a String
167 * Ignores trailing whitespace and newlines
169 * @param msg String to be decoded
171 * @return the decoded value in byte[] form
174 public static byte[] decode64( String msg )
176 return decode64( msg.getBytes());
179 // Returns the number of bytes converted
181 decode64( byte ret[], int ret_off, byte a, byte b, byte c, byte d )
183 byte da = dec_table[a];
184 byte db = dec_table[b];
185 byte dc = dec_table[c];
186 byte dd = dec_table[d];
188 if (da == -1 || db == -1 || (dc == -1 && c != 0x3d) || (dd == -1 && d != 0x3d))
189 throw new IllegalArgumentException("Invalid character ["+a+", "+b+", "+c+", "+d+"]");
191 ret[ret_off++] = (byte)(da << 2 | db >>> 4);
192 if (c == 0x3d) // Ascii =
194 ret[ret_off++] = (byte)(db << 4 | dc >>> 2);
195 if (d == 0x3d) // Ascii =
197 ret[ret_off++] = (byte)(dc << 6 | dd);