package md5;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
/**
* MD5值十六进制显示,适用于文件和字符串
* @author 张明亮
* @date 2009.10.08
*/
public class MD5Util {
/**
* 字符串MD5加密
* @param s
* @return
*/
public static String stringToMD5(String str) {
try {
byte[] strTemp = str.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
return toHexString(mdTemp.digest());
} catch (Exception e) {
return null;
}
}
/**
* 根据文件名获得文件MD5
* @param fileName
* @return
*/
public static String fileNameToMD5(String fileName) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(fileName);
return streamToMD5(inputStream);
} catch (Exception e) {
return null;
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 根据输入流获得文件MD5
* @param inputStream
* @return
*/
public static String streamToMD5(InputStream inputStream) {
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = inputStream.read(buffer)) > 0) {
mdTemp.update(buffer, 0, numRead);
}
return toHexString(mdTemp.digest());
} catch (Exception e) {
return null;
}
}
private static String toHexString(byte[] md) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
int j = md.length;
char str[] = new char[j * 2];
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[2 * i] = hexDigits[byte0 >>> 4 & 0xf];
str[i * 2 + 1] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
public static void main(String[] args) {
//字符串加密
System.err.println(MD5Util.stringToMD5("XX"));
//文件加密
System.err.println(MD5Util.fileNameToMD5("f://imp.log"));
}
}