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 */ 019package org.apache.shiro.lang.codec; 020 021 022/** 023 * <a href="http://en.wikipedia.org/wiki/Hexadecimal">Hexadecimal</a> encoder and decoder. 024 * <p/> 025 * This class was borrowed from Apache Commons Codec SVN repository (rev. {@code 560660}) with modifications 026 * to enable Hex conversion without a full dependency on Commons Codec. We didn't want to reinvent the wheel of 027 * great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar 028 * <p/> 029 * As per the Apache 2.0 license, the original copyright notice and all author and copyright information have 030 * remained in tact. 031 * 032 * @see <a href="http://en.wikipedia.org/wiki/Hexadecimal">Wikipedia: Hexadecimal</a> 033 * @since 0.9 034 */ 035@SuppressWarnings("checkstyle:MagicNumber") 036public final 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 private Hex() { 047 048 } 049 050 /** 051 * Encodes the specified byte array to a character array and then returns that character array 052 * as a String. 053 * 054 * @param bytes the byte array to Hex-encode. 055 * @return A String representation of the resultant hex-encoded char array. 056 */ 057 public static String encodeToString(byte[] bytes) { 058 char[] encodedChars = encode(bytes); 059 return new String(encodedChars); 060 } 061 062 /** 063 * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 064 * The returned array will be double the length of the passed array, as it takes two characters to represent any 065 * given byte. 066 * 067 * @param data byte[] to convert to Hex characters 068 * @return A char[] containing hexadecimal characters 069 */ 070 public static char[] encode(byte[] data) { 071 072 int l = data.length; 073 074 char[] out = new char[l << 1]; 075 076 // two characters form the hex value. 077 for (int i = 0, j = 0; i < l; i++) { 078 out[j++] = DIGITS[(0xF0 & data[i]) >>> 4]; 079 out[j++] = DIGITS[0x0F & data[i]]; 080 } 081 082 return out; 083 } 084 085 /** 086 * Converts an array of character bytes representing hexadecimal values into an 087 * array of bytes of those same values. The returned array will be half the 088 * length of the passed array, as it takes two characters to represent any 089 * given byte. An exception is thrown if the passed char array has an odd 090 * number of elements. 091 * 092 * @param array An array of character bytes containing hexadecimal digits 093 * @return A byte array containing binary data decoded from 094 * the supplied byte array (representing characters). 095 * @throws IllegalArgumentException Thrown if an odd number of characters is supplied 096 * to this function 097 * @see #decode(char[]) 098 */ 099 public static byte[] decode(byte[] array) throws IllegalArgumentException { 100 String s = CodecSupport.toString(array); 101 return decode(s); 102 } 103 104 /** 105 * Converts the specified Hex-encoded String into a raw byte array. This is a 106 * convenience method that merely delegates to {@link #decode(char[])} using the 107 * argument's hex.toCharArray() value. 108 * 109 * @param hex a Hex-encoded String. 110 * @return A byte array containing binary data decoded from the supplied String's char array. 111 */ 112 public static byte[] decode(String hex) { 113 return decode(hex.toCharArray()); 114 } 115 116 /** 117 * Converts an array of characters representing hexadecimal values into an 118 * array of bytes of those same values. The returned array will be half the 119 * length of the passed array, as it takes two characters to represent any 120 * given byte. An exception is thrown if the passed char array has an odd 121 * number of elements. 122 * 123 * @param data An array of characters containing hexadecimal digits 124 * @return A byte array containing binary data decoded from 125 * the supplied char array. 126 * @throws IllegalArgumentException if an odd number or illegal of characters 127 * is supplied 128 */ 129 @SuppressWarnings("magicNumber") 130 public static byte[] decode(char[] data) throws IllegalArgumentException { 131 132 int len = data.length; 133 134 if ((len & 0x01) != 0) { 135 throw new IllegalArgumentException("Odd number of characters."); 136 } 137 138 byte[] out = new byte[len >> 1]; 139 140 // two characters form the hex value. 141 for (int i = 0, j = 0; j < len; i++) { 142 int f = toDigit(data[j], j) << 4; 143 j++; 144 f = f | toDigit(data[j], j); 145 j++; 146 out[i] = (byte) (f & 0xFF); 147 } 148 149 return out; 150 } 151 152 /** 153 * Converts a hexadecimal character to an integer. 154 * 155 * @param ch A character to convert to an integer digit 156 * @param index The index of the character in the source 157 * @return An integer 158 * @throws IllegalArgumentException if ch is an illegal hex character 159 */ 160 protected static int toDigit(char ch, int index) throws IllegalArgumentException { 161 int digit = Character.digit(ch, 2 << 3); 162 if (digit == -1) { 163 throw new IllegalArgumentException("Illegal hexadecimal character " + ch + " at index " + index); 164 } 165 return digit; 166 } 167 168 169}