I would like to get the hash of files (mostly video files) independent of external properties such as path and file name. I'll be needing to store hash in a database and compare file hash to find identical files.
Have a look at the DigestInputStream class: http://docs.oracle.com/javase/7/docs/api/java/security/DigestInputStream.html
public byte[] digestFile( File f ){
  try {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    FileInputStream fis = new FileInputStream( f );
    byte[] buffer = new byte[1024];
    int read = -1;
    while ((read = fis.read(buffer)) != -1) {
      messageDigest.digest(buffer, 0, read);
    }
    return messageDigest.digest();
  } catch (VariousExceptions e) {
    //handle
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With