import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.Arrays;
public class HexCodeUtils {
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
String str = "測試";
byte[] digest = md.digest(str.getBytes());
System.out.println(HexCodeUtils.toHexString(digest));
//db06c78d1e24cf708a14ce81c9b617ec
byte[] bytes = HexCodeUtils.decode("db06c78d1e24cf708a14ce81c9b617ec");
System.out.println(Arrays.equals(digest, bytes));
}
private static final HexEncoder encoder = new HexEncoder();
public static String toHexString(byte[] data) throws IOException {
return toHexString(data, 0, data.length);
}
public static String toHexString(byte[] data, int off, int length) throws IOException {
byte[] encoded = encode(data, off, length);
return new String(encoded);
}
public static byte[] encode(byte[] data) throws IOException {
return encode(data, 0, data.length);
}
public static byte[] encode(byte[] data, int off, int length) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
encoder.encode(data, off, length, bOut);
return bOut.toByteArray();
}
public static int encode(byte[] data, OutputStream out) throws IOException {
return encoder.encode(data, 0, data.length, out);
}
public static int encode(byte[] data, int off, int length, OutputStream out) throws IOException {
return encoder.encode(data, off, length, out);
}
public static byte[] decode(byte[] data) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
encoder.decode(data, 0, data.length, bOut);
return bOut.toByteArray();
}
public static byte[] decode(String data) throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
encoder.decode(data, bOut);
return bOut.toByteArray();
}
public static int decode(String data, OutputStream out) throws IOException {
return encoder.decode(data, out);
}
}
class HexEncoder {
private final byte[] encodingTable = {
(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f'
};
private final byte[] decodingTable = new byte[128];
private void initialiseDecodingTable() {
for ( int i = 0; i < decodingTable.length; i++ ) {
decodingTable[i] = (byte) 0xff;
}
for ( int i = 0; i < encodingTable.length; i++ ) {
decodingTable[encodingTable[i]] = (byte) i;
}
decodingTable['A'] = decodingTable['a'];
decodingTable['B'] = decodingTable['b'];
decodingTable['C'] = decodingTable['c'];
decodingTable['D'] = decodingTable['d'];
decodingTable['E'] = decodingTable['e'];
decodingTable['F'] = decodingTable['f'];
}
public HexEncoder() {
initialiseDecodingTable();
}
private static boolean ignore(char c) {
return c == '\n' || c == '\r' || c == '\t' || c == ' ';
}
public int encode(byte[] data, int off, int length, OutputStream out) throws IOException {
for ( int i = off; i < (off + length); i++ ) {
int v = data[i] & 0xff;
out.write(encodingTable[(v >>> 4)]);
out.write(encodingTable[v & 0xf]);
}
return length * 2;
}
public int decode(byte[] data, int off, int length, OutputStream out) throws IOException {
byte b1, b2;
int outLen = 0;
int end = off + length;
while ( end > off ) {
if ( !ignore((char) data[end - 1]) ) break;
end--;
}
int i = off;
while ( i < end ) {
while ( i < end && ignore((char) data[i]) ) i++;
b1 = decodingTable[data[i++]];
while ( i < end && ignore((char) data[i]) ) i++;
b2 = decodingTable[data[i++]];
if ( (b1 | b2) < 0 ) throw new IOException("invalid characters encountered in Hex data");
out.write((b1 << 4) | b2);
outLen++;
}
return outLen;
}
public int decode(String data, OutputStream out) throws IOException {
byte b1, b2;
int length = 0;
int end = data.length();
while ( end > 0 ) {
if ( !ignore(data.charAt(end - 1)) ) break;
end--;
}
int i = 0;
while ( i < end ) {
while ( i < end && ignore(data.charAt(i)) ) i++;
b1 = decodingTable[data.charAt(i++)];
while ( i < end && ignore(data.charAt(i)) ) i++;
b2 = decodingTable[data.charAt(i++)];
if ( (b1 | b2) < 0 ) throw new IOException("invalid characters encountered in Hex string");
out.write((b1 << 4) | b2);
length++;
}
return length;
}
}
[Java]字節數組互轉Hex編碼工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.