1 package edu.internet2.middleware.shibboleth.common;
4 * The Bouncy Castle License
6 * Copyright (c) 2000 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9 * and associated documentation files (the "Software"), to deal in the Software without restriction,
10 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
28 private static final byte[] encodingTable =
30 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
31 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
32 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
33 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
34 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
35 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
36 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
38 (byte)'w', (byte)'x', (byte)'y', (byte)'z',
39 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6',
40 (byte)'7', (byte)'8', (byte)'9',
45 * encode the input data producong a base 64 encoded byte array.
47 * @return a byte array containing the base 64 encoded data.
49 public static byte[] encode(
54 int modulus = data.length % 3;
57 bytes = new byte[4 * data.length / 3];
61 bytes = new byte[4 * ((data.length / 3) + 1)];
64 int dataLength = (data.length - modulus);
66 for (int i = 0, j = 0; i < dataLength; i += 3, j += 4)
69 a2 = data[i + 1] & 0xff;
70 a3 = data[i + 2] & 0xff;
72 bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
73 bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
74 bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
75 bytes[j + 3] = encodingTable[a3 & 0x3f];
79 * process the tail end.
86 case 0: /* nothing left to do */
89 d1 = data[data.length - 1] & 0xff;
90 b1 = (d1 >>> 2) & 0x3f;
91 b2 = (d1 << 4) & 0x3f;
93 bytes[bytes.length - 4] = encodingTable[b1];
94 bytes[bytes.length - 3] = encodingTable[b2];
95 bytes[bytes.length - 2] = (byte)'=';
96 bytes[bytes.length - 1] = (byte)'=';
99 d1 = data[data.length - 2] & 0xff;
100 d2 = data[data.length - 1] & 0xff;
102 b1 = (d1 >>> 2) & 0x3f;
103 b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
104 b3 = (d2 << 2) & 0x3f;
106 bytes[bytes.length - 4] = encodingTable[b1];
107 bytes[bytes.length - 3] = encodingTable[b2];
108 bytes[bytes.length - 2] = encodingTable[b3];
109 bytes[bytes.length - 1] = (byte)'=';
117 * set up the decoding table.
119 private static final byte[] decodingTable;
123 decodingTable = new byte[128];
125 for (int i = 'A'; i <= 'Z'; i++)
127 decodingTable[i] = (byte)(i - 'A');
130 for (int i = 'a'; i <= 'z'; i++)
132 decodingTable[i] = (byte)(i - 'a' + 26);
135 for (int i = '0'; i <= '9'; i++)
137 decodingTable[i] = (byte)(i - '0' + 52);
140 decodingTable['+'] = 62;
141 decodingTable['/'] = 63;
145 * decode the base 64 encoded input data.
147 * @return a byte array representing the decoded data.
149 public static byte[] decode(
155 if (data[data.length - 2] == '=')
157 bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
159 else if (data[data.length - 1] == '=')
161 bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
165 bytes = new byte[((data.length / 4) * 3)];
168 for (int i = 0, j = 0; i < data.length - 4; i += 4, j += 3)
170 b1 = decodingTable[data[i]];
171 b2 = decodingTable[data[i + 1]];
172 b3 = decodingTable[data[i + 2]];
173 b4 = decodingTable[data[i + 3]];
175 bytes[j] = (byte)((b1 << 2) | (b2 >> 4));
176 bytes[j + 1] = (byte)((b2 << 4) | (b3 >> 2));
177 bytes[j + 2] = (byte)((b3 << 6) | b4);
180 if (data[data.length - 2] == '=')
182 b1 = decodingTable[data[data.length - 4]];
183 b2 = decodingTable[data[data.length - 3]];
185 bytes[bytes.length - 1] = (byte)((b1 << 2) | (b2 >> 4));
187 else if (data[data.length - 1] == '=')
189 b1 = decodingTable[data[data.length - 4]];
190 b2 = decodingTable[data[data.length - 3]];
191 b3 = decodingTable[data[data.length - 2]];
193 bytes[bytes.length - 2] = (byte)((b1 << 2) | (b2 >> 4));
194 bytes[bytes.length - 1] = (byte)((b2 << 4) | (b3 >> 2));
198 b1 = decodingTable[data[data.length - 4]];
199 b2 = decodingTable[data[data.length - 3]];
200 b3 = decodingTable[data[data.length - 2]];
201 b4 = decodingTable[data[data.length - 1]];
203 bytes[bytes.length - 3] = (byte)((b1 << 2) | (b2 >> 4));
204 bytes[bytes.length - 2] = (byte)((b2 << 4) | (b3 >> 2));
205 bytes[bytes.length - 1] = (byte)((b3 << 6) | b4);
212 * decode the base 64 encoded String data.
214 * @return a byte array representing the decoded data.
216 public static byte[] decode(
222 if (data.charAt(data.length() - 2) == '=')
224 bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
226 else if (data.charAt(data.length() - 1) == '=')
228 bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
232 bytes = new byte[((data.length() / 4) * 3)];
235 for (int i = 0, j = 0; i < data.length() - 4; i += 4, j += 3)
237 b1 = decodingTable[data.charAt(i)];
238 b2 = decodingTable[data.charAt(i + 1)];
239 b3 = decodingTable[data.charAt(i + 2)];
240 b4 = decodingTable[data.charAt(i + 3)];
242 bytes[j] = (byte)((b1 << 2) | (b2 >> 4));
243 bytes[j + 1] = (byte)((b2 << 4) | (b3 >> 2));
244 bytes[j + 2] = (byte)((b3 << 6) | b4);
247 if (data.charAt(data.length() - 2) == '=')
249 b1 = decodingTable[data.charAt(data.length() - 4)];
250 b2 = decodingTable[data.charAt(data.length() - 3)];
252 bytes[bytes.length - 1] = (byte)((b1 << 2) | (b2 >> 4));
254 else if (data.charAt(data.length() - 1) == '=')
256 b1 = decodingTable[data.charAt(data.length() - 4)];
257 b2 = decodingTable[data.charAt(data.length() - 3)];
258 b3 = decodingTable[data.charAt(data.length() - 2)];
260 bytes[bytes.length - 2] = (byte)((b1 << 2) | (b2 >> 4));
261 bytes[bytes.length - 1] = (byte)((b2 << 4) | (b3 >> 2));
265 b1 = decodingTable[data.charAt(data.length() - 4)];
266 b2 = decodingTable[data.charAt(data.length() - 3)];
267 b3 = decodingTable[data.charAt(data.length() - 2)];
268 b4 = decodingTable[data.charAt(data.length() - 1)];
270 bytes[bytes.length - 3] = (byte)((b1 << 2) | (b2 >> 4));
271 bytes[bytes.length - 2] = (byte)((b2 << 4) | (b3 >> 2));
272 bytes[bytes.length - 1] = (byte)((b3 << 6) | b4);