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。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM