package md5;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
/**
* 对文件全文生成MD5摘要
*
* @param file
* 要加密的文件
* @return MD5摘要码
*/
static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
@SuppressWarnings("unused")
public static String method1(File file) {
FileInputStream fis = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
fis = new FileInputStream(file);
FileChannel fChannel = fis.getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024);
int length = -1;
while ((length = fChannel.read(buffer)) != -1) {
buffer.flip();
md.update(buffer);
buffer.compact();
}
byte[] b = md.digest();
return byteToHexString(b);
// 16位加密
// return buf.toString().substring(8, 24);
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 把byte[]数组转换成十六进制字符串表示形式
*
* @param tmp
* 要转换的byte[]
* @return 十六进制字符串表示形式
*/
public static String byteToHexString(byte[] tmp) {
String s;
// 用字节表示就是 16 个字节
char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
// 所以表示成 16 进制需要 32 个字符
int k = 0; // 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
// 转换成 16 进制字符的转换
byte byte0 = tmp[i]; // 取第 i 个字节
str[k++] = hexdigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
// >>> 为逻辑右移,将符号位一起右移
str[k++] = hexdigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
}
s = new String(str); // 换后的结果转换为字符串
return s;
}
public static String method2(File inputFile) throws IOException {
// 缓冲区大小(这个可以抽出一个参数)
int bufferSize = 1024 * 1024;
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null;
try {
// 拿到一个MD5转换器(同样,这里可以换成SHA1)
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
// 使用DigestInputStream
fileInputStream = new FileInputStream(inputFile);
digestInputStream = new DigestInputStream(fileInputStream,
messageDigest);
// read的过程中进行MD5处理,直到读完文件
byte[] buffer = new byte[bufferSize];
while (digestInputStream.read(buffer) > 0)
;
// 获取最终的MessageDigest
messageDigest = digestInputStream.getMessageDigest();
// 拿到结果,也是字节数组,包含16个元素
byte[] resultByteArray = messageDigest.digest();
// 同样,把字节数组转换成字符串
return byteToHexString(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
} finally {
try {
digestInputStream.close();
} catch (Exception e) {
}
try {
fileInputStream.close();
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
File file = new File(
"F:/迅雷下载/CentOS-7-x86_64-Minimal-1611.iso");
System.out.println(method1(file));
} catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime) / 1000);
}
}
转载于:https://my.oschina.net/zjg23/blog/843801