Java comes with MD5 checksum file

Source: Internet
Author: User

http://www.iteye.com/topic/1127319

The day before yesterday the first blog to the forum, about the Java file Monitoring article, post address in: http://www.iteye.com/topic/1127281

Comment on a lot of friends, download code of Friends very much, thank the forum to see my post friends, as well as reply to the comments of friends, to provide me with suggestions of friends.

From these suggestions, although the language is short, but there are some words, here to say a bit about the post code in Hashfile in the MD5 file check algorithm,

The algorithm is the use of Java MessageDigest class, test results, to obtain a 2G file MD5 code, time-consuming 971 seconds, this efficiency is too power, can be described with the pit father, so with MD5 file check code to determine whether the file is modified, for small files may also be appropriate, If it's a big file, all right, hit the Wall and die!

The code in the Hashfile is this:

Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.security.MessageDigest;

public class Hashfile {

/**
* @param args
*/
public static char[] Hexchar = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};

public static string Gethash (String fileName, String hashtype)
Throws Exception {
InputStream fis;
FIS = new FileInputStream (fileName);
byte[] buffer = new byte[1024];
MessageDigest MD5 = messagedigest.getinstance (Hashtype);
int numread = 0;
while ((Numread = fis.read (buffer)) > 0) {
Md5.update (buffer, 0, numread);
}
Fis.close ();
Return tohexstring (Md5.digest ());
}

public static String tohexstring (byte[] b) {
StringBuilder sb = new StringBuilder (B.length * 2);
for (int i = 0; i < b.length; i++) {
Sb.append (hexchar[(B[i] & 0xf0) >>> 4]);
Sb.append (Hexchar[b[i] & 0x0f]);
}
return sb.tostring ();
}
}

Test results:


True to force ah, more than 2G, efficiency into this!

Well, the MD5 algorithm that comes with it, is fooled, for checking whether the file is updated this problem, now I use the solution is the file class's LastModified method, the code so

private String Gethash (string fp) {
File File = new file (FP);
Return string.valueof (File.lastmodified ());
}

By comparing the last modification time of the file to determine whether the file is updated, the large file is also easy to win,

The test result is this:



Certainly there are different solutions for different problems.

Analyze the original Hashfile code, get the bottleneck of MD5 check code is appearing in

Java code
  1. Public static String Gethash (String fileName, string hashtype)
  2. throws Exception {
  3. InputStream fis;
  4. FIS = new FileInputStream (FileName);
  5. byte[] buffer = new byte[1024];
  6. MessageDigest MD5 = messagedigest.getinstance (Hashtype);
  7. int numread = 0;
  8. While ((Numread = fis.read (buffer)) > 0) { //bottleneck
  9. Md5.update (buffer, 0, Numread);
  10. }
  11. Fis.close ();
  12. return tohexstring (Md5.digest ());
  13. }
public static string Gethash (String fileName, String hashtype)              throws Exception {          InputStream fis;          FIS = new FileInputStream (fileName);          byte[] buffer = new byte[1024];          MessageDigest MD5 = messagedigest.getinstance (hashtype);          int numread = 0;          while ((Numread = fis.read (buffer)) > 0) {  //bottleneck            md5.update (buffer, 0, numread);          }          Fis.close ();          Return tohexstring (Md5.digest ());      }  

In the above code, while loop n times, 2G file, loop 1024 * 1024 * 2 times, do not give force!

Chimer reply

A simple version of NIO, see you're always suspicious of Java slow

C + + MD5 tool validation results:

File:k:\games\world of Warcraft\data\common. MPQ
size:2226587191 bytes
modified:2008 Wednesday, November 19, 12:57:24
md5:cd9f9c5523f3ba3866b81ccc74ed6476


Java Run result, MS
Time: 12672,cd9f9c5523f3ba3866b81ccc74ed6476


Core code

String Hashtype = "MD5";
FileInputStream fstream = null;
try {
MessageDigest MD5 = messagedigest.getinstance (Hashtype);
FStream = new FileInputStream (
"K:\\games\\world of Warcraft\\scan.dll");
"K:\\games\\world of Warcraft\\data\\patch-3. MPQ ");
"K:\\games\\world of Warcraft\\data\\common. MPQ ");
FileChannel Fchannel = Fstream.getchannel ();
Bytebuffer buffer = bytebuffer.allocate (8*1024);
Long s = System.currenttimemillis ();
for (int count = fchannel.read (buffer); count!=-1; count = fchannel.read (buffer)
) {
Buffer.flip ();
Md5.update (buffer);
if (!buffer.hasremaining ()) {
System.out.println ("Count:" +count);
Buffer.clear ();
}
}
s = System.currenttimemillis ()-S;
System.out.println ("Time-consuming:" +s+ "," +getstring (Md5.digest ()));

} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}finally{
try {
if (fstream!=null)
Fstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Java comes with MD5 checksum file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.