I have to fingerprint files to match doublets. What is recommended with Java in 2013? Should I also compare the file size, or is this a unnecessary check?
The probability of false positive should be very close to 0
EDIT: Lots of answers, thanks. What is the standard of backup software today? SHA-256? higher? I guess md5 is not suitable?
Are you asking how to getting the md5 checksums of files in Java? If that's the case then read the accepted answers here and here. Basically, do this:
It is unnecessary.
If the probability of false positives has to be zero, as opposed to "lower than the probability you will be struck by lightning," then no hash algorithm at all can be used; you must compare the files byte by byte.
For what it's worth, if you can use third-party libraries, you can use Guava to compare two files byte-by-byte with the one-liner
which takes care of opening and closing the files as well as the details of comparison.
If you're willing to accept false positives that are less likely than getting struck by lightning, then you could do
which returns a
HashCode
, and then you can test that for equality with the hash of another file. (That version also deals with the messiness ofMessageDigest
, of opening and closing the file properly, etcetera.)