Posted on 2012-06-01 17:57
IceWee 閱讀(3498)
評論(1) 編輯 收藏 所屬分類:
Java 、
加解密
之前使用到了NIO的FileChannel做文件快速閱讀,后來發(fā)現(xiàn)存在一個巨大的BUG,使用它會一直不釋放文件句柄,即生成MD5的文件不能操作(移動或刪除等),這個BUG網(wǎng)上吵得沸沸揚揚,至今沒有解決,畢竟是SUN的BUG,解鈴還需系鈴人啊!咱只好乖乖的使用文件分塊讀取的方法,這種方式要求生成MD5和驗證的時候得使用相同的緩存大小。MD5Utils.java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/** *//**
* <p>
* MD5工具類
* </p>
*
* @author IceWee
* @date 2012-5-15
* @version 1.0
*/

public class MD5Utils
{
private static final String ALGORIGTHM_MD5 = "MD5";
private static final int CACHE_SIZE = 2048;

/** *//**
* <p>
* 字符串生成MD5
* </p>
*
* @param input
* @return
* @throws Exception
*/

public static String createMD5(String input) throws Exception
{
return createMD5(input, null);
}

/** *//**
* <p>
* 字符串生成MD5
* </p>
*
* @param input
* @param charset 編碼(可選)
* @return
* @throws Exception
*/

public static String createMD5(String input, String charset) throws Exception
{
byte[] data;

if (charset != null && !"".equals(charset))
{
data = input.getBytes(charset);

} else
{
data = input.getBytes();
}
MessageDigest messageDigest = getMD5();
messageDigest.update(data);
return byteArrayToHexString(messageDigest.digest());
}

/** *//**
* <p>
* 生成文件MD5
* </p>
* <p>
* 該方法中使用的FileChannel存在一個巨大Bug,不釋放文件句柄,即生成MD5的文件無法操作(移動或刪除等)<br>
* 該方法已被generateFileMD5取代
* </p>
*
* @param filePath
* @return
* @throws Exception
*/
@Deprecated

public static String createFileMD5(String filePath) throws Exception
{
String md5 = "";
File file = new File(filePath);

if (file.exists())
{
MessageDigest messageDigest = getMD5();
FileInputStream in = new FileInputStream(file);
FileChannel fileChannel = in.getChannel();
MappedByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messageDigest.update(byteBuffer);
fileChannel.close();
in.close();
byte data[] = messageDigest.digest();
md5 = byteArrayToHexString(data);
}
return md5;
}

/** *//**
* <p>
* 生成文件MD5值
* <p>
* <p>
* 在進(jìn)行文件校驗時,文件讀取的緩沖大小[CACHE_SIZE]需與該方法的一致,否則校驗失敗
* </p>
*
* @param filePath
* @return
* @throws Exception
*/

public static String generateFileMD5(String filePath) throws Exception
{
String md5 = "";
File file = new File(filePath);

if (file.exists())
{
MessageDigest messageDigest = getMD5();
InputStream in = new FileInputStream(file);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;

while ((nRead = in.read(cache)) != -1)
{
messageDigest.update(cache, 0, nRead);
}
in.close();
byte data[] = messageDigest.digest();
md5 = byteArrayToHexString(data);
}
return md5;
}

/** *//**
* <p>
* MD5摘要字節(jié)數(shù)組轉(zhuǎn)換為16進(jìn)制字符串
* </p>
*
* @param data MD5摘要
* @return
*/

private static String byteArrayToHexString(byte[] data)
{
// 用來將字節(jié)轉(zhuǎn)換成 16 進(jìn)制表示的字符

char hexDigits[] =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
// 每個字節(jié)用 16 進(jìn)制表示的話,使用兩個字符,所以表示成 16 進(jìn)制需要 32 個字符
char arr[] = new char[16 * 2];
int k = 0; // 表示轉(zhuǎn)換結(jié)果中對應(yīng)的字符位置
// 從第一個字節(jié)開始,對 MD5 的每一個字節(jié)轉(zhuǎn)換成 16 進(jìn)制字符的轉(zhuǎn)換

for (int i = 0; i < 16; i++)
{
byte b = data[i]; // 取第 i 個字節(jié)
// 取字節(jié)中高 4 位的數(shù)字轉(zhuǎn)換, >>>為邏輯右移,將符號位一起右移
arr[k++] = hexDigits[b >>> 4 & 0xf];
// 取字節(jié)中低 4 位的數(shù)字轉(zhuǎn)換
arr[k++] = hexDigits[b & 0xf];
}
// 換后的結(jié)果轉(zhuǎn)換為字符串
return new String(arr);
}

/** *//**
* <p>
* 獲取MD5實例
* </p>
*
* @return
* @throws NoSuchAlgorithmException
*/

private static MessageDigest getMD5() throws NoSuchAlgorithmException
{
return MessageDigest.getInstance(ALGORIGTHM_MD5);
}
}
