001: /**
002: * Base64 - encode/decode data using the Base64 encoding scheme
003: * Copyright (c) 1998 by Kevin Kelley
004: * Contact: Kevin Kelley <kelley@ruralnet.net>
005: * 30718 Rd. 28, La Junta, CO, 81050 USA.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or 1any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: */package org.objectweb.jonas.security.realm.lib;
023:
024: /**
025: * Provides encoding of raw bytes to base64-encoded characters, and
026: * decoding of base64 characters to raw bytes.
027: *
028: * @author Kevin Kelley (kelley@ruralnet.net)
029: * @version 1.3
030: * @date 06 August 1998
031: * @modified 14 February 2000
032: * @modified 22 September 2000
033: */
034: public class Base64 {
035:
036: /**
037: * code characters for values 0..63
038: */
039: private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
040: .toCharArray();
041:
042: /**
043: * lookup table for converting base64 characters to value in range 0..63
044: */
045: private static byte[] codes = new byte[256];
046:
047: static {
048: for (int i = 0; i < 256; i++) {
049: codes[i] = -1;
050: }
051: for (int i = 'A'; i <= 'Z'; i++) {
052: codes[i] = (byte) (i - 'A');
053: }
054: for (int i = 'a'; i <= 'z'; i++) {
055: codes[i] = (byte) (26 + i - 'a');
056: }
057: for (int i = '0'; i <= '9'; i++) {
058: codes[i] = (byte) (52 + i - '0');
059: }
060: codes['+'] = 62;
061: codes['/'] = 63;
062: }
063:
064: /**
065: * returns an array of base64-encoded characters to represent the
066: * passed data array.
067: *
068: * @param data the array of bytes to encode
069: * @return base64-coded character array.
070: */
071: public static char[] encode(byte[] data) {
072: char[] out = new char[((data.length + 2) / 3) * 4];
073:
074: //
075: // 3 bytes encode to 4 chars. Output is always an even
076: // multiple of 4 characters.
077: //
078: for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
079: boolean quad = false;
080: boolean trip = false;
081:
082: int val = (0xFF & (int) data[i]);
083: val <<= 8;
084: if ((i + 1) < data.length) {
085: val |= (0xFF & (int) data[i + 1]);
086: trip = true;
087: }
088: val <<= 8;
089: if ((i + 2) < data.length) {
090: val |= (0xFF & (int) data[i + 2]);
091: quad = true;
092: }
093: out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
094: val >>= 6;
095: out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
096: val >>= 6;
097: out[index + 1] = alphabet[val & 0x3F];
098: val >>= 6;
099: out[index + 0] = alphabet[val & 0x3F];
100: }
101: return out;
102: }
103:
104: /**
105: * Decodes a BASE-64 encoded stream to recover the original
106: * data. White space before and after will be trimmed away,
107: * but no other manipulation of the input will be performed.
108: *
109: * As of version 1.2 this method will properly handle input
110: * containing junk characters (newlines and the like) rather
111: * than throwing an error. It does this by pre-parsing the
112: * input and generating from that a count of VALID input
113: * characters.
114: * @param data BASE-64 encoded stream
115: * @return original data
116: **/
117: public static byte[] decode(char[] data) {
118: // as our input could contain non-BASE64 data (newlines,
119: // whitespace of any sort, whatever) we must first adjust
120: // our count of USABLE data so that...
121: // (a) we don't misallocate the output array, and
122: // (b) think that we miscalculated our data length
123: // just because of extraneous throw-away junk
124:
125: int tempLen = data.length;
126: for (int ix = 0; ix < data.length; ix++) {
127: if ((data[ix] > 255) || codes[data[ix]] < 0) {
128: --tempLen; // ignore non-valid chars and padding
129: }
130: }
131: // calculate required length:
132: // -- 3 bytes for every 4 valid base64 chars
133: // -- plus 2 bytes if there are 3 extra base64 chars,
134: // or plus 1 byte if there are 2 extra.
135:
136: int len = (tempLen / 4) * 3;
137: if ((tempLen % 4) == 3) {
138: len += 2;
139: }
140: if ((tempLen % 4) == 2) {
141: len += 1;
142: }
143:
144: byte[] out = new byte[len];
145:
146: int shift = 0; // # of excess bits stored in accum
147: int accum = 0; // excess bits
148: int index = 0;
149:
150: // we now go through the entire array (NOT using the 'tempLen' value)
151: for (int ix = 0; ix < data.length; ix++) {
152: int value = (data[ix] > 255) ? -1 : codes[data[ix]];
153:
154: if (value >= 0) { // skip over non-code
155: accum <<= 6; // bits shift up by 6 each time thru
156: shift += 6; // loop, with new bits being put in
157: accum |= value; // at the bottom.
158: if (shift >= 8) { // whenever there are 8 or more shifted in,
159: shift -= 8; // write them out (from the top, leaving any
160: out[index++] = // excess at the bottom for next iteration.
161: (byte) ((accum >> shift) & 0xff);
162: }
163: }
164: // we will also have skipped processing a padding null byte ('=') here;
165: // these are used ONLY for padding to an even length and do not legally
166: // occur as encoded data. for this reason we can ignore the fact that
167: // no index++ operation occurs in that special case: the out[] array is
168: // initialized to all-zero bytes to start with and that works to our
169: // advantage in this combination.
170: }
171:
172: // if there is STILL something wrong we just have to throw up now!
173: if (index != out.length) {
174: throw new Error("Miscalculated data length (wrote " + index
175: + " instead of " + out.length + ")");
176: }
177:
178: return out;
179: }
180:
181: }
|