View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.io.UnsupportedEncodingException;
19  
20  import org.apache.commons.lang3.CharSet;
21  import org.apache.commons.lang3.StringUtils;
22  
23  /**
24   * A few (highly inefficient) methods for converting <code>String's</code> into the hex for a given encoding and back again.
25   */
26  public class HexUtils {
27  
28  	private static final String ZERO = "0";
29  	private static final int BYTE_MASK = 0x000000ff;
30  	private static final String[] HEX_RANGES = new String[] { "0-9", "A-F", "a-f" };
31  	private static final String HEX_RANGES_STRING = toString(HEX_RANGES);
32  	private static final CharSet HEX_CHARSET = CharSet.getInstance(HEX_RANGES);
33  
34  	public static final CharSet getHexCharSet() {
35  		return HEX_CHARSET;
36  	}
37  
38  	public static final String[] getHexRanges() {
39  		return HEX_RANGES;
40  	}
41  
42  	protected static final String toString(String[] tokens) {
43  		StringBuilder sb = new StringBuilder();
44  		sb.append("[");
45  		for (int i = 0; i < HEX_RANGES.length; i++) {
46  			if (i != 0) {
47  				sb.append(",");
48  			}
49  			sb.append(HEX_RANGES[i]);
50  		}
51  		sb.append("]");
52  		return sb.toString();
53  	}
54  
55  	/**
56  	 * Convert <code>string</code> into a <code>byte[]</code> using the specified encoding, then convert each <code>byte</code> into its 2
57  	 * digit hexadecimal form.
58  	 */
59  	public static String toHexString(String string, String encoding) throws UnsupportedEncodingException {
60  		byte[] bytes = encoding == null ? string.getBytes() : string.getBytes(encoding);
61  		return toHexString(bytes);
62  	}
63  
64  	/**
65  	 * Convert each <code>byte</code> into its 2 digit hexadecimal form.
66  	 */
67  	public static String toHexString(byte[] bytes) {
68  		StringBuilder sb = new StringBuilder();
69  		for (byte b : bytes) {
70  			int masked = BYTE_MASK & b;
71  			String hex = Integer.toHexString(masked).toUpperCase();
72  			String padded = StringUtils.leftPad(hex, 2, ZERO);
73  			sb.append(padded);
74  		}
75  		return sb.toString();
76  	}
77  
78  	/**
79  	 * Return true if every character is valid hex <code>0-9</code>, <code>a-f</code>, or <code>A-F</code>
80  	 */
81  	public static final boolean isHex(char... chars) {
82  		for (char c : chars) {
83  			if (!HEX_CHARSET.contains(c)) {
84  				return false;
85  			}
86  		}
87  		return true;
88  	}
89  
90  	/**
91  	 * Given a string in <code>strictly hex</code> format, return the corresponding <code>byte[]</code>. <code>strictly hex</code> in the
92  	 * context of this method means that the string:<br>
93  	 * 1 - Contains only the characters <code>a-f</code>, <code>A-F</code>, and <code>0-9</code><br>
94  	 * 2 - Its length is an even number.
95  	 */
96  	public static final byte[] getBytesFromHexString(String hex) {
97  		char[] chars = hex.toCharArray();
98  		int length = chars.length;
99  		if (length % 2 != 0) {
100 			throw new IllegalArgumentException("Invalid hex string [" + hex + "].  String must contain an even number of characters.  " + length + " is not an even number!");
101 		}
102 		byte[] bytes = new byte[length / 2];
103 		int byteIndex = 0;
104 		for (int i = 0; i < length; i += 2) {
105 			char c1 = chars[i];
106 			char c2 = chars[i + 1];
107 			String s = c1 + "" + c2;
108 			if (!isHex(c1, c2)) {
109 				int byteNumber = i / 2 + 1;
110 				throw new IllegalArgumentException("Invalid hex string [" + hex + "].  Invalid hex detected at byte " + byteNumber + " [" + s
111 				        + "].  Both characters must be in the range " + HEX_RANGES_STRING);
112 			}
113 			int integer = Integer.parseInt(s, 16);
114 			int masked = integer & BYTE_MASK;
115 			byte b = (byte) masked;
116 			bytes[byteIndex++] = b;
117 		}
118 		return bytes;
119 	}
120 
121 	/**
122 	 * Given a string in <code>strictly hex</code> format and the <code>encoding</code> that was used to produce the hex, convert it back to
123 	 * a Java <code>String</code>. <code>strictly hex</code> in the context of this method means that the string:<br>
124 	 * 1 - Contains only the characters <code>a-f</code>, <code>A-F</code>, and <code>0-9</code><br>
125 	 * 2 - Its length is an even number.
126 	 */
127 	public static final String toStringFromHex(String hex, String encoding) throws UnsupportedEncodingException {
128 		byte[] bytes = getBytesFromHexString(hex);
129 		return StringUtils.toString(bytes, encoding);
130 	}
131 
132 }