001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.codec;
020
021 /**
022 * <a href="http://en.wikipedia.org/wiki/Hexadecimal">Hexadecimal</a> encoder and decoder.
023 * <p/>
024 * This class was borrowed from Apache Commons Codec SVN repository (rev. {@code 560660}) with modifications
025 * to enable Hex conversion without a full dependency on Commons Codec. We didn't want to reinvent the wheel of
026 * great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar
027 * <p/>
028 * As per the Apache 2.0 license, the original copyright notice and all author and copyright information have
029 * remained in tact.
030 *
031 * @author Apache Software Foundation
032 * @author Les Hazlewood
033 * @see <a href="http://en.wikipedia.org/wiki/Hexadecimal">Wikipedia: Hexadecimal</a>
034 * @since 0.9
035 */
036 public class Hex {
037
038 /**
039 * Used to build output as Hex
040 */
041 private static final char[] DIGITS = {
042 '0', '1', '2', '3', '4', '5', '6', '7',
043 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
044 };
045
046 /**
047 * Encodes the specifed byte array to a character array and then returns that character array
048 * as a String.
049 *
050 * @param bytes the byte array to Hex-encode.
051 * @return A String representation of the resultant hex-encoded char array.
052 */
053 public static String encodeToString(byte[] bytes) {
054 char[] encodedChars = encode(bytes);
055 return new String(encodedChars);
056 }
057
058 /**
059 * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
060 * The returned array will be double the length of the passed array, as it takes two characters to represent any
061 * given byte.
062 *
063 * @param data byte[] to convert to Hex characters
064 * @return A char[] containing hexidecimal characters
065 */
066 public static char[] encode(byte[] data) {
067
068 int l = data.length;
069
070 char[] out = new char[l << 1];
071
072 // two characters form the hex value.
073 for (int i = 0, j = 0; i < l; i++) {
074 out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
075 out[j++] = DIGITS[0x0F & data[i]];
076 }
077
078 return out;
079 }
080
081 /**
082 * Converts an array of character bytes representing hexidecimal values into an
083 * array of bytes of those same values. The returned array will be half the
084 * length of the passed array, as it takes two characters to represent any
085 * given byte. An exception is thrown if the passed char array has an odd
086 * number of elements.
087 *
088 * @param array An array of character bytes containing hexidecimal digits
089 * @return A byte array containing binary data decoded from
090 * the supplied byte array (representing characters).
091 * @throws IllegalArgumentException Thrown if an odd number of characters is supplied
092 * to this function
093 * @see #decode(char[])
094 */
095 public static byte[] decode(byte[] array) throws IllegalArgumentException {
096 String s = CodecSupport.toString(array);
097 return decode(s);
098 }
099
100 /**
101 * Converts the specified Hex-encoded String into a raw byte array. This is a
102 * convenience method that merely delegates to {@link #decode(char[])} using the
103 * argument's hex.toCharArray() value.
104 *
105 * @param hex a Hex-encoded String.
106 * @return A byte array containing binary data decoded from the supplied String's char array.
107 */
108 public static byte[] decode(String hex) {
109 return decode(hex.toCharArray());
110 }
111
112 /**
113 * Converts an array of characters representing hexidecimal values into an
114 * array of bytes of those same values. The returned array will be half the
115 * length of the passed array, as it takes two characters to represent any
116 * given byte. An exception is thrown if the passed char array has an odd
117 * number of elements.
118 *
119 * @param data An array of characters containing hexidecimal digits
120 * @return A byte array containing binary data decoded from
121 * the supplied char array.
122 * @throws IllegalArgumentException if an odd number or illegal of characters
123 * is supplied
124 */
125 public static byte[] decode(char[] data) throws IllegalArgumentException {
126
127 int len = data.length;
128
129 if ((len & 0x01) != 0) {
130 throw new IllegalArgumentException("Odd number of characters.");
131 }
132
133 byte[] out = new byte[len >> 1];
134
135 // two characters form the hex value.
136 for (int i = 0, j = 0; j < len; i++) {
137 int f = toDigit(data[j], j) << 4;
138 j++;
139 f = f | toDigit(data[j], j);
140 j++;
141 out[i] = (byte) (f & 0xFF);
142 }
143
144 return out;
145 }
146
147 /**
148 * Converts a hexadecimal character to an integer.
149 *
150 * @param ch A character to convert to an integer digit
151 * @param index The index of the character in the source
152 * @return An integer
153 * @throws IllegalArgumentException if ch is an illegal hex character
154 */
155 protected static int toDigit(char ch, int index) throws IllegalArgumentException {
156 int digit = Character.digit(ch, 16);
157 if (digit == -1) {
158 throw new IllegalArgumentException("Illegal hexadecimal charcter " + ch + " at index " + index);
159 }
160 return digit;
161 }
162
163
164 }