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
}
}
Depending on what you need, you can do this pretty easily using Guava's Files and ByteStreams classes:
byte[] digest = Files.getDigest(file, MessageDigest.getInstance("SHA"));
精彩评论