MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能
2008-08-02 17:53
627 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能。
* @author 狂人三号
*/
public class MD5Util
{
/**
* 16进制字符集
*/
private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* 指定算法为MD5的MessageDigest
*/
private static MessageDigest messageDigest = null;
/**
* 初始化messageDigest的加密算法为MD5
*/
static
{
try
{
messageDigest = MessageDigest.getInstance("MD5");
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
/**
* 获取文件的MD5值
* @param file 目标文件
* @return MD5字符串
*/
public static String getFileMD5String(File file)
{
String ret = "";
FileInputStream in = null;
FileChannel ch = null;
try
{
in = new FileInputStream(file);
ch = in.getChannel();
ByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messageDigest.update(byteBuffer);
ret = bytesToHex(messageDigest.digest());
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(ch != null)
{
try
{
ch.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
return ret;
}
/**
* 获取文件的MD5值
* @param fileName 目标文件的完整名称
* @return MD5字符串
*/
public static String getFileMD5String(String fileName)
{
return getFileMD5String(new File(fileName));
}
/**
* MD5加密字符串
* @param str 目标字符串
* @return MD5加密后的字符串
*/
public static String getMD5String(String str)
{
return getMD5String(str.getBytes());
}
/**
* MD5加密以byte数组表示的字符串
* @param bytes 目标byte数组
* @return MD5加密后的字符串
*/
public static String getMD5String(byte[] bytes)
{
messageDigest.update(bytes);
return bytesToHex(messageDigest.digest());
}
/**
* 校验密码与其MD5是否一致
* @param pwd 密码字符串
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkPassword(String pwd, String md5)
{
return getMD5String(pwd).equalsIgnoreCase(md5);
}
/**
* 校验密码与其MD5是否一致
* @param pwd 以字符数组表示的密码
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkPassword(char[] pwd, String md5)
{
return checkPassword(new String(pwd), md5);
}
/**
* 检验文件的MD5值
* @param file 目标文件
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkFileMD5(File file, String md5)
{
return getFileMD5String(file).equalsIgnoreCase(md5);
}
/**
* 检验文件的MD5值
* @param fileName 目标文件的完整名称
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkFileMD5(String fileName, String md5)
{
return checkFileMD5(new File(fileName), md5);
}
/**
* 将字节数组转换成16进制字符串
* @param bytes 目标字节数组
* @return 转换结果
*/
public static String bytesToHex(byte bytes[])
{
return bytesToHex(bytes, 0, bytes.length);
}
/**
* 将字节数组中指定区间的子数组转换成16进制字符串
* @param bytes 目标字节数组
* @param start 起始位置(包括该位置)
* @param end 结束位置(不包括该位置)
* @return 转换结果
*/
public static String bytesToHex(byte bytes[], int start, int end)
{
StringBuilder sb = new StringBuilder();
for(int i = start; i < start + end; i++)
{
sb.append(byteToHex(bytes[i]));
}
return sb.toString();
}
/**
* 将单个字节码转换成16进制字符串
* @param bt 目标字节
* @return 转换结果
*/
public static String byteToHex(byte bt)
{
return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[bt & 0xf];
}
public static void main(String[] args) throws IOException
{
long begin = System.currentTimeMillis();
String md5 = getFileMD5String(new File("F://003.rar"));
long end = System.currentTimeMillis();
System.out.println("MD5:/t" + md5 + "/nTime:/t" + (end - begin) + "ms");
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能。
* @author 狂人三号
*/
public class MD5Util
{
/**
* 16进制字符集
*/
private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* 指定算法为MD5的MessageDigest
*/
private static MessageDigest messageDigest = null;
/**
* 初始化messageDigest的加密算法为MD5
*/
static
{
try
{
messageDigest = MessageDigest.getInstance("MD5");
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
/**
* 获取文件的MD5值
* @param file 目标文件
* @return MD5字符串
*/
public static String getFileMD5String(File file)
{
String ret = "";
FileInputStream in = null;
FileChannel ch = null;
try
{
in = new FileInputStream(file);
ch = in.getChannel();
ByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messageDigest.update(byteBuffer);
ret = bytesToHex(messageDigest.digest());
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
if(ch != null)
{
try
{
ch.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
return ret;
}
/**
* 获取文件的MD5值
* @param fileName 目标文件的完整名称
* @return MD5字符串
*/
public static String getFileMD5String(String fileName)
{
return getFileMD5String(new File(fileName));
}
/**
* MD5加密字符串
* @param str 目标字符串
* @return MD5加密后的字符串
*/
public static String getMD5String(String str)
{
return getMD5String(str.getBytes());
}
/**
* MD5加密以byte数组表示的字符串
* @param bytes 目标byte数组
* @return MD5加密后的字符串
*/
public static String getMD5String(byte[] bytes)
{
messageDigest.update(bytes);
return bytesToHex(messageDigest.digest());
}
/**
* 校验密码与其MD5是否一致
* @param pwd 密码字符串
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkPassword(String pwd, String md5)
{
return getMD5String(pwd).equalsIgnoreCase(md5);
}
/**
* 校验密码与其MD5是否一致
* @param pwd 以字符数组表示的密码
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkPassword(char[] pwd, String md5)
{
return checkPassword(new String(pwd), md5);
}
/**
* 检验文件的MD5值
* @param file 目标文件
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkFileMD5(File file, String md5)
{
return getFileMD5String(file).equalsIgnoreCase(md5);
}
/**
* 检验文件的MD5值
* @param fileName 目标文件的完整名称
* @param md5 基准MD5值
* @return 检验结果
*/
public static boolean checkFileMD5(String fileName, String md5)
{
return checkFileMD5(new File(fileName), md5);
}
/**
* 将字节数组转换成16进制字符串
* @param bytes 目标字节数组
* @return 转换结果
*/
public static String bytesToHex(byte bytes[])
{
return bytesToHex(bytes, 0, bytes.length);
}
/**
* 将字节数组中指定区间的子数组转换成16进制字符串
* @param bytes 目标字节数组
* @param start 起始位置(包括该位置)
* @param end 结束位置(不包括该位置)
* @return 转换结果
*/
public static String bytesToHex(byte bytes[], int start, int end)
{
StringBuilder sb = new StringBuilder();
for(int i = start; i < start + end; i++)
{
sb.append(byteToHex(bytes[i]));
}
return sb.toString();
}
/**
* 将单个字节码转换成16进制字符串
* @param bt 目标字节
* @return 转换结果
*/
public static String byteToHex(byte bt)
{
return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[bt & 0xf];
}
public static void main(String[] args) throws IOException
{
long begin = System.currentTimeMillis();
String md5 = getFileMD5String(new File("F://003.rar"));
long end = System.currentTimeMillis();
System.out.println("MD5:/t" + md5 + "/nTime:/t" + (end - begin) + "ms");
}
}
相关文章推荐
- MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能
- MD5工具类,提供字符串MD5加密(校验)、文件MD5值获取(校验)功能
- OC获取文件MD5值与swift字符串MD5加密方法
- MD5工具类 -- 文件MD5值 -- MD5加密
- C#实现字符串,文件获取Md5加密校验方法
- 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值
- MD5工具类 -- 文件MD5值 -- MD5加密
- html5的file api以及配合spark-md5获取文件md5值
- JAVA获取字符串或者文件的MD5值
- linux shell 命令获取字符串/文件的MD5值
- iOS使用MD5 - 字符串加密至MD5&获取文件MD5
- C#获取文件MD5值或者字符串MD5值方法
- Java利用MessageDigest获取字符串或文件MD5详解
- java 获取字符串或文件的md5值
- linux shell 命令获取字符串/文件的MD5值
- android 字符串工具类 格式化日期字符串 拼接数组 判断字符串是否为空 转换时间显示 转换文件大小 获取中英文字符个数
- linux shell 命令获取字符串/文件的MD5值
- nginx获取大文件MD5值(nginx模块ngx_file_md5)
- [转载]Linux系统如何用shell命令获取文件或字符串的MD5值
- Android初级教程:对文件和字符串进行MD5加密工具类