Posted on 2012-06-01 17:57 
IceWee 阅读(3515) 
评论(1)  编辑  收藏  所属分类: 
Java 、
加解密 
			 
			
		 
		
 
之前使用到了NIO的FileChannel做文件快速阅读,后来发现存在一个巨大的BUG,使用它会一直不释放文件句柄,即生成MD5的文件不能操作(移动或删除等),这个BUG网上吵得沸沸扬扬,至今没有解决,毕竟是SUN的BUG,解铃还需系铃人啊!咱只好乖乖的使用文件分块读取的方法,这种方式要求生成MD5和验证的时候得使用相同的缓存大小。MD5Utils.java
 import java.io.File;
import java.io.File;
 import java.io.FileInputStream;
import java.io.FileInputStream;
 import java.io.InputStream;
import java.io.InputStream;
 import java.nio.MappedByteBuffer;
import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel;
 import java.security.MessageDigest;
import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
import java.security.NoSuchAlgorithmException;


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

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

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

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

 /** *//**
    /** *//**
 * <p>
     * <p>
 * 字符串生成MD5
     * 字符串生成MD5
 * </p>
     * </p>
 *
     * 
 * @param input
     * @param input
 * @param charset 编码(可选)
     * @param charset 编码(可选)
 * @return
     * @return
 * @throws Exception
     * @throws Exception
 */
     */

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

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

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

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

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

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

 /** *//**
    /** *//**
 * <p>
     * <p>
 * 生成文件MD5值
     * 生成文件MD5值
 * <p>
     * <p>
 * <p>
     * <p>
 * 在进行文件校验时,文件读取的缓冲大小[CACHE_SIZE]需与该方法的一致,否则校验失败
     * 在进行文件校验时,文件读取的缓冲大小[CACHE_SIZE]需与该方法的一致,否则校验失败
 * </p>
     * </p>
 *
     * 
 * @param filePath
     * @param filePath
 * @return
     * @return
 * @throws Exception
     * @throws Exception
 */
     */

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

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

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

 /** *//**
    /** *//**
 * <p>
     * <p>
 * MD5摘要字节数组转换为16进制字符串
     * MD5摘要字节数组转换为16进制字符串
 * </p>
     * </p>
 *
     * 
 * @param data MD5摘要
     * @param data MD5摘要
 * @return
     * @return
 */
     */

 private static String byteArrayToHexString(byte[] data)
    private static String byteArrayToHexString(byte[] data)  {
{
 // 用来将字节转换成 16 进制表示的字符
        // 用来将字节转换成 16 进制表示的字符

 char hexDigits[] =
        char hexDigits[] =  {
{
 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 
 };
        };
 // 每个字节用 16 进制表示的话,使用两个字符,所以表示成 16 进制需要 32 个字符
        // 每个字节用 16 进制表示的话,使用两个字符,所以表示成 16 进制需要 32 个字符
 char arr[] = new char[16 * 2];
        char arr[] = new char[16 * 2];
 int k = 0; // 表示转换结果中对应的字符位置
        int k = 0; // 表示转换结果中对应的字符位置
 // 从第一个字节开始,对 MD5 的每一个字节转换成 16 进制字符的转换
        // 从第一个字节开始,对 MD5 的每一个字节转换成 16 进制字符的转换

 for (int i = 0; i < 16; i++)
        for (int i = 0; i < 16; i++)  {
{
 byte b = data[i]; // 取第 i 个字节
            byte b = data[i]; // 取第 i 个字节
 // 取字节中高 4 位的数字转换, >>>为逻辑右移,将符号位一起右移
            // 取字节中高 4 位的数字转换, >>>为逻辑右移,将符号位一起右移
 arr[k++] = hexDigits[b >>> 4 & 0xf];
            arr[k++] = hexDigits[b >>> 4 & 0xf];
 // 取字节中低 4 位的数字转换
            // 取字节中低 4 位的数字转换
 arr[k++] = hexDigits[b & 0xf];
            arr[k++] = hexDigits[b & 0xf];
 }
        }
 // 换后的结果转换为字符串
        // 换后的结果转换为字符串
 return new String(arr);
        return new String(arr);
 }
    }
 
    

 /** *//**
    /** *//**
 * <p>
     * <p>
 * 获取MD5实例
     * 获取MD5实例
 * </p>
     * </p>
 *
     * 
 * @return
     * @return
 * @throws NoSuchAlgorithmException
     * @throws NoSuchAlgorithmException 
 */
     */

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