JAVA笔记 文件HASH

     public static void main(String args[]) {
           try {
               System.out.println(getMD5Checksum("RationalRoseEnterpriseEditionforWindows.2003.06.00.391.000.exe"));
           }
           catch (Exception e) {
               e.printStackTrace();
           }
       }
    
      public static byte[] createChecksum(String filename) throws Exception {
           InputStream fis =  new FileInputStream(filename);

           byte[] buffer = new byte[1024];
           MessageDigest complete = MessageDigest.getInstance("MD5");
           int numRead;

           do {
               numRead = fis.read(buffer);
               if (numRead > 0) {
                   complete.update(buffer, 0, numRead);
               }
           } while (numRead != -1);

           fis.close();
           return complete.digest();
       }
    
      public static String getMD5Checksum(String filename) throws Exception {
           byte[] b = createChecksum(filename);
           String result = "";

           for (int i=0; i < b.length; i++) {
               result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
           }
           return result;
       }
   

运行结果

E615BE40376123D27D0436AAE42477DE

在网上用校验工具试过了,结果OK。

你可能感兴趣的:(JAVA笔记 文件HASH)