thieumao

Check MD5 file

Sep 14th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package test;
  6.  
  7. /**
  8.  *
  9.  * @author leo
  10.  */
  11. import java.io.FileInputStream;
  12. import java.security.MessageDigest;
  13.  
  14. public class MD5CheckSumExample {
  15.  
  16.     @SuppressWarnings("empty-statement")
  17.     public static void main(String[] args) throws Exception {
  18.         MessageDigest md = MessageDigest.getInstance("MD5");
  19.         FileInputStream fis = new FileInputStream("D:\\Coding\\HashCalc.exe");
  20.  
  21.         byte[] dataBytes = new byte[1024];
  22.  
  23.         int nread = 0;
  24.         while ((nread = fis.read(dataBytes)) != -1) {
  25.             md.update(dataBytes, 0, nread);
  26.         };
  27.         byte[] mdbytes = md.digest();
  28.  
  29.         //convert the byte to hex format method 1
  30.         StringBuffer sb = new StringBuffer();
  31.         for (int i = 0; i < mdbytes.length; i++) {
  32.             sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
  33.         }
  34.  
  35.         System.out.println("Digest(in hex format):: " + sb.toString());
  36.  
  37.         //convert the byte to hex format method 2
  38. //        StringBuffer hexString = new StringBuffer();
  39. //        for (int i = 0; i < mdbytes.length; i++) {
  40. //            String hex = Integer.toHexString(0xff & mdbytes[i]);
  41. //            if (hex.length() == 1) {
  42. //                hexString.append('0');
  43. //            }
  44. //            hexString.append(hex);
  45. //        }
  46. //        System.out.println("Digest(in hex format):: " + hexString.toString());
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment