Archive for November 2009

Leverage Checksum to determine identical files

An easy way to identify if two files are exactly the same or not is by determining the check sum of each file and comparing the check sum. If the check sum for both the files is exactly same then it can said that the two files are identical.

I have written a little java program which determines the check sum of file.


import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class Checksum {

public static String computeCheckSum(String fileName) {

try {
FileInputStream stream = new FileInputStream(fileName);
MessageDigest md = MessageDigest.getInstance(”SHA-1″);

DigestInputStream dIS = new DigestInputStream(stream, md);
dIS.on(true);

byte [] b = new byte[1000];

while (dIS.read(b,0,1000) > -1);

byte []signature = md.digest();
//System.out.println(”The signature is :”);
for (int i = 0; i < signature.length; i++) {
System.out.print(signature[i]);
}
} catch (Exception e) {
e.printStackTrace();
}

return "";
}

public static void main(String [] a) {
Checksum.computeCheckSum(a[0]);
}

}

|