49 lines
950 B
Java
49 lines
950 B
Java
|
|
||
|
|
||
|
import java.security.MessageDigest;
|
||
|
import java.security.NoSuchAlgorithmException;
|
||
|
|
||
|
public class HexUtil
|
||
|
{
|
||
|
public static void printHex(byte[] bytes)
|
||
|
{
|
||
|
for(byte b : bytes) System.out.print(byteToHex(b));
|
||
|
}
|
||
|
|
||
|
public static String byteToHex(byte data) {
|
||
|
|
||
|
StringBuffer buf = new StringBuffer();
|
||
|
buf.append(toHexChar((data >>> 4) & 0x0F));
|
||
|
buf.append(toHexChar(data & 0x0F));
|
||
|
|
||
|
return buf.toString();
|
||
|
}
|
||
|
|
||
|
public static char toHexChar(int i) {
|
||
|
if ((0 <= i) && (i <= 9)) {
|
||
|
return (char) ('0' + i);
|
||
|
} else {
|
||
|
return (char) ('a' + (i - 10));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static byte[] generateMD5(byte[] data)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
byte[] md5 = MessageDigest.getInstance("MD5").digest(data);
|
||
|
String m="";
|
||
|
for(int i=0; i<md5.length; i++)
|
||
|
{
|
||
|
m = m + byteToHex(md5[i]).toUpperCase();
|
||
|
}
|
||
|
return m.getBytes();
|
||
|
} catch (NoSuchAlgorithmException e)
|
||
|
{
|
||
|
// TODO Auto-generated catch block
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
}
|