java實現md5的驗證

最近在百度做系統的研發,遇到一個文件md5校驗的問題,剛開始以爲挺複雜,後來才發現其實前人已經爲我們鋪好了道路,而且還是康莊大道啊,謝謝前輩們。需求是這樣的,系統需要通過ftp下載一個服務器上的兩份文件(一份文件是源文件,另一份用於校對的md5),通過對文件md5的校驗後,比對一起下載下來的這個md5文件,如果相同則文件是安全的,我們在進行下一步操作,這是爲了防止文件被別有用心的人篡改。其實我們要做的其實主要就是對該源文件進行md5碼的生成。代碼如下所示:

  1. /** 
  2.  * MD5驗證工具 
  3.  *  
  4.  * @author yangchao 
  5.  * @version $Id: MD5Util.java, v 0.1 2012-2-20 下午4:06:11 yangchao Exp $ 
  6.  */  
  7. public class MD5Util {  
  8.   
  9.     /** 
  10.      * 默認的密碼字符串組合,用來將字節轉換成 16 進製表示的字符,apache校驗下載的文件的正確性用的就是默認的這個組合 
  11.      */  
  12.     protected static char          hexDigits[]   = { '0''1''2''3''4''5''6''7''8''9''a''b''c',  
  13.             'd''e''f'                       };  
  14.     protected static MessageDigest messagedigest = null;  
  15.     static {  
  16.         try {  
  17.             messagedigest = MessageDigest.getInstance("MD5");  
  18.         } catch (NoSuchAlgorithmException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.   
  23.     public static String getFileMD5String(File file) throws IOException {  
  24.         InputStream fis;  
  25.         fis = new FileInputStream(file);  
  26.         byte[] buffer = new byte[1024];  
  27.         int numRead = 0;  
  28.         while ((numRead = fis.read(buffer)) > 0) {  
  29.             messagedigest.update(buffer, 0, numRead);  
  30.         }  
  31.         fis.close();  
  32.         return bufferToHex(messagedigest.digest());  
  33.     }  
  34.   
  35.     private static String bufferToHex(byte bytes[]) {  
  36.         return bufferToHex(bytes, 0, bytes.length);  
  37.     }  
  38.   
  39.     private static String bufferToHex(byte bytes[], int m, int n) {  
  40.         StringBuffer stringbuffer = new StringBuffer(2 * n);  
  41.         int k = m + n;  
  42.         for (int l = m; l < k; l++) {  
  43.             appendHexPair(bytes[l], stringbuffer);  
  44.         }  
  45.         return stringbuffer.toString();  
  46.     }  
  47.   
  48.     private static void appendHexPair(byte bt, StringBuffer stringbuffer) {  
  49.         char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字節中高 4 位的數字轉換   
  50.         // 爲邏輯右移,將符號位一起右移,此處未發現兩種符號有何不同   
  51.         char c1 = hexDigits[bt & 0xf];// 取字節中低 4 位的數字轉換   
  52.         stringbuffer.append(c0);  
  53.         stringbuffer.append(c1);  
  54.     }  
  55.   
  56.     public static void main(String[] args) throws IOException {  
  57.         File file = new File("E:/test/crm_account_YYYY_MM_DD.txt");  
  58.         String md5 = getFileMD5String(file);  
  59.         System.out.println("md5:" + md5);  
  60.     }  
  61.   
  62.   
  63. }  
/**
 * 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文件比對,就可以了,簡單吧

發佈了27 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章