Blame view

src/main/java/com/phxl/common/util/MD5Util.java 2.93 KB
Quilan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
package com.phxl.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 提供一系列获取和校验MD5的方法
 *
 */
public class MD5Util {

  /**
   * 获取该输入流的MD5值
   * 
   * @param is
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static String getMD5(InputStream is) throws NoSuchAlgorithmException, IOException {
    StringBuffer md5 = new StringBuffer();
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] dataBytes = new byte[1024];
    
    int nread = 0; 
    while ((nread = is.read(dataBytes)) != -1) {
      md.update(dataBytes, 0, nread);
    };
    byte[] mdbytes = md.digest();
    
    // convert the byte to hex format
    for (int i = 0; i < mdbytes.length; i++) {
      md5.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
    }
    return md5.toString();
  }
  
  /**
   * 获取该文件的MD5值
   * 
   * @param file
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static String getMD5(File file) throws NoSuchAlgorithmException, IOException {
    FileInputStream fis = new FileInputStream(file);
    return getMD5(fis);
  }
  
  /**
   * 获取指定路径文件的MD5值
   * 
   * @param path
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static String getMD5(String path) throws NoSuchAlgorithmException, IOException {
    FileInputStream fis = new FileInputStream(path);
    return getMD5(fis);
  }

  /**
   * 校验该输入流的MD5值
   * 
   * @param is
   * @param toBeCheckSum
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static boolean md5CheckSum(InputStream is, String toBeCheckSum) throws NoSuchAlgorithmException, IOException {
    return getMD5(is).equals(toBeCheckSum);
  }
  
  /**
   * 校验该文件的MD5值
   * 
   * @param file
   * @param toBeCheckSum
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static boolean md5CheckSum(File file, String toBeCheckSum) throws NoSuchAlgorithmException, IOException {
    return getMD5(file).equals(toBeCheckSum);
  }
  
  /**
   * 校验指定路径文件的MD5值
   * 
   * @param path
   * @param toBeCheckSum
   * @return
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static boolean md5CheckSum(String path, String toBeCheckSum) throws NoSuchAlgorithmException, IOException {
    return getMD5(path).equals(toBeCheckSum);
  }
  
  /* MD5 TEST
  public static void main(String[] args) {
    try {
      System.out.println(getMD5("D:/work/BJ/ESB_CSS_CSS_ImportCustCompFormRejectInfoSrv.xml"));
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  */
}