最近在百度做系統的研發,遇到一個文件md5校驗的問題,剛開始以爲挺複雜,後來才發現其實前人已經爲我們鋪好了道路,而且還是康莊大道啊,謝謝前輩們。需求是這樣的,系統需要通過ftp下載一個服務器上的兩份文件(一份文件是源文件,另一份用於校對的md5),通過對文件md5的校驗後,比對一起下載下來的這個md5文件,如果相同則文件是安全的,我們在進行下一步操作,這是爲了防止文件被別有用心的人篡改。其實我們要做的其實主要就是對該源文件進行md5碼的生成。代碼如下所示:
-
-
-
-
-
-
- public class MD5Util {
-
-
-
-
- protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
- 'd', 'e', 'f' };
- protected static MessageDigest messagedigest = null;
- static {
- try {
- messagedigest = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- }
-
- public static String getFileMD5String(File file) throws IOException {
- InputStream fis;
- fis = new FileInputStream(file);
- byte[] buffer = new byte[1024];
- int numRead = 0;
- while ((numRead = fis.read(buffer)) > 0) {
- messagedigest.update(buffer, 0, numRead);
- }
- fis.close();
- return bufferToHex(messagedigest.digest());
- }
-
- private static String bufferToHex(byte bytes[]) {
- return bufferToHex(bytes, 0, bytes.length);
- }
-
- private static String bufferToHex(byte bytes[], int m, int n) {
- StringBuffer stringbuffer = new StringBuffer(2 * n);
- int k = m + n;
- for (int l = m; l < k; l++) {
- appendHexPair(bytes[l], stringbuffer);
- }
- return stringbuffer.toString();
- }
-
- private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
- char c0 = hexDigits[(bt & 0xf0) >> 4];
-
- char c1 = hexDigits[bt & 0xf];
- stringbuffer.append(c0);
- stringbuffer.append(c1);
- }
-
- public static void main(String[] args) throws IOException {
- File file = new File("E:/test/crm_account_YYYY_MM_DD.txt");
- String md5 = getFileMD5String(file);
- System.out.println("md5:" + md5);
- }
-
-
- }
/**
* MD5驗證工具
*
* @author yangchao
* @version $Id: MD5Util.java, v 0.1 2012-2-20 下午4:06:11 yangchao Exp $
*/
public class MD5Util {
/**
* 默認的密碼字符串組合,用來將字節轉換成 16 進製表示的字符,apache校驗下載的文件的正確性用的就是默認的這個組合
*/
protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
'd', 'e', 'f' };
protected static MessageDigest messagedigest = null;
static {
try {
messagedigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String getFileMD5String(File file) throws IOException {
InputStream fis;
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
messagedigest.update(buffer, 0, numRead);
}
fis.close();
return bufferToHex(messagedigest.digest());
}
private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}
private static String bufferToHex(byte bytes[], int m, int n) {
StringBuffer stringbuffer = new StringBuffer(2 * n);
int k = m + n;
for (int l = m; l < k; l++) {
appendHexPair(bytes[l], stringbuffer);
}
return stringbuffer.toString();
}
private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字節中高 4 位的數字轉換
// 爲邏輯右移,將符號位一起右移,此處未發現兩種符號有何不同
char c1 = hexDigits[bt & 0xf];// 取字節中低 4 位的數字轉換
stringbuffer.append(c0);
stringbuffer.append(c1);
}
public static void main(String[] args) throws IOException {
File file = new File("E:/test/crm_account_YYYY_MM_DD.txt");
String md5 = getFileMD5String(file);
System.out.println("md5:" + md5);
}
}
運行一下就會生成一串md5碼,在於md5文件比對,就可以了,簡單吧