001: /*
002: * $Header$
003: * $Revision: 33 $
004: * $Date: 2004-07-22 18:15:30 +0200 (Thu, 22 Jul 2004) $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package com.sabre.security.jndi.util;
065:
066: import java.io.ByteArrayOutputStream;
067:
068: /**
069: * Library of utility methods useful in dealing with converting byte arrays
070: * to and from strings of hexadecimal digits.
071: *
072: * @author Craig R. McClanahan
073: */
074:
075: public class HexUtils {
076: // Code from Ajp11, from Apache's JServ
077:
078: // Table for HEX to DEC byte translation
079: public static final int[] DEC = { -1, -1, -1, -1, -1, -1, -1, -1,
080: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
081: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
082: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 00, 01, 02, 03, 04,
083: 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12,
084: 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
085: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10,
086: 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
087: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
088: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
089: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
090: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
091: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
092: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
093: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
094: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
095: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
096: -1, -1, -1, -1, -1, -1, -1, -1, };
097:
098: /**
099: * Convert a String of hexadecimal digits into the corresponding
100: * byte array by encoding each two hexadecimal digits as a byte.
101: *
102: * @param digits Hexadecimal digits representation
103: *
104: * @exception java.lang.IllegalArgumentException if an invalid hexadecimal digit
105: * is found, or the input string contains an odd number of hexadecimal
106: * digits
107: */
108: public static byte[] convert(String digits) {
109:
110: ByteArrayOutputStream baos = new ByteArrayOutputStream();
111: for (int i = 0; i < digits.length(); i += 2) {
112: char c1 = digits.charAt(i);
113: if ((i + 1) >= digits.length())
114: throw new IllegalArgumentException("hexUtil.odd");
115: char c2 = digits.charAt(i + 1);
116: byte b = 0;
117: if ((c1 >= '0') && (c1 <= '9'))
118: b += ((c1 - '0') * 16);
119: else if ((c1 >= 'a') && (c1 <= 'f'))
120: b += ((c1 - 'a' + 10) * 16);
121: else if ((c1 >= 'A') && (c1 <= 'F'))
122: b += ((c1 - 'A' + 10) * 16);
123: else
124: throw new IllegalArgumentException("hexUtil.bad");
125: if ((c2 >= '0') && (c2 <= '9'))
126: b += (c2 - '0');
127: else if ((c2 >= 'a') && (c2 <= 'f'))
128: b += (c2 - 'a' + 10);
129: else if ((c2 >= 'A') && (c2 <= 'F'))
130: b += (c2 - 'A' + 10);
131: else
132: throw new IllegalArgumentException("hexUtil.bad");
133: baos.write(b);
134: }
135: return (baos.toByteArray());
136:
137: }
138:
139: /**
140: * Convert a byte array into a printable format containing a
141: * String of hexadecimal digit characters (two per byte).
142: *
143: * @param bytes Byte array representation
144: */
145: public static String convert(byte bytes[]) {
146:
147: StringBuffer sb = new StringBuffer(bytes.length * 2);
148: for (int i = 0; i < bytes.length; i++) {
149: sb.append(convertDigit((int) (bytes[i] >> 4)));
150: sb.append(convertDigit((int) (bytes[i] & 0x0f)));
151: }
152: return (sb.toString());
153:
154: }
155:
156: /**
157: * Convert 4 hex digits to an int, and return the number of converted
158: * bytes.
159: *
160: * @param hex Byte array containing exactly four hexadecimal digits
161: *
162: * @exception java.lang.IllegalArgumentException if an invalid hexadecimal digit
163: * is included
164: */
165: public static int convert2Int(byte[] hex) {
166: // Code from Ajp11, from Apache's JServ
167:
168: // assert b.length==4
169: // assert valid data
170: int len;
171: if (hex.length < 4)
172: return 0;
173: if (DEC[hex[0]] < 0)
174: throw new IllegalArgumentException("hexUtil.bad");
175: len = DEC[hex[0]];
176: len = len << 4;
177: if (DEC[hex[1]] < 0)
178: throw new IllegalArgumentException("hexUtil.bad");
179: len += DEC[hex[1]];
180: len = len << 4;
181: if (DEC[hex[2]] < 0)
182: throw new IllegalArgumentException("hexUtil.bad");
183: len += DEC[hex[2]];
184: len = len << 4;
185: if (DEC[hex[3]] < 0)
186: throw new IllegalArgumentException("hexUtil.bad");
187: len += DEC[hex[3]];
188: return len;
189: }
190:
191: /**
192: * [Private] Convert the specified value (0 .. 15) to the corresponding
193: * hexadecimal digit.
194: *
195: * @param value Value to be converted
196: */
197: private static char convertDigit(int value) {
198:
199: value &= 0x0f;
200: if (value >= 10)
201: return ((char) (value - 10 + 'a'));
202: else
203: return ((char) (value + '0'));
204:
205: }
206:
207: }
|