📄 md5.java
字号:
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author KO
*/
/**
* MD5类
*
* @author KO
*/
public class MD5 {
FileChannel fc = null;
RandomAccessFile fis = null;
MappedByteBuffer mbb = null;
/**
* 函数名:fileToString 功能:构建文件内存映射
*
* @param filePath -
* 文件地址
* @return - 文件内存映射
*/
public ByteBuffer fileToString(String filePath){
File f = new File(filePath);
try {
fis = new RandomAccessFile(f,"rw");
} catch (FileNotFoundException ex) {
throw new RuntimeException("要用MD5加密的文件路径不存在");
}
fc = fis.getChannel();
try {
mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
} catch (IOException ex) {
throw new RuntimeException("创建内存映射失败");
}
return mbb;
}
/**
* 函数名:calcMD5
*
* @param b -
* 文件内存映射
* @return 文件MD5值
*/
public static void main(String args[]){
MD5 md5 = new MD5();
try {
System.out.println(md5.calcMD5(md5.fileToString("e:\\ds.exe")));
} catch (MD5Exception ex) {
Logger.getLogger(MD5.class.getName()).log(Level.SEVERE,null, ex);
}
}
public String calcMD5(ByteBuffer b){
MessageDigest alga = null;
String tmp = null;
try {
alga = MessageDigest.getInstance("MD5");
alga.update(b);
byte[] digesta = alga.digest();
tmp = byte2hex(digesta);
return tmp;
}
catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("计算MD5失败!");
}finally{
try {
if (fc != null) {
fc.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
throw new RuntimeException("关闭文件管道资源失败!");
}
try {
if (mbb != null) {
unmap(mbb);
}
} catch (Exception ex) {
throw new RuntimeException("释放文件内存映射失败!");
}
return tmp;
}
}
/**
* 函数名:byte2hex 功能:二行制转十六制字符串
*
* @param b -
* 二进制流
* @return 十六制字符串
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
if (n < b.length - 1) {
hs = hs + "";
}
}
// return hs.toUpperCase();
return hs;
}
/**
* 函数名:unmap 功能:释放指定资源
*
* @param buffer -
* 要释放的资源
* @throws java.lang.Exception
*/
public static void unmap(final Object buffer) throws Exception {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod(
"cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -