How to generate Hash value of any type of file (.doc,.docx,*.pdf, etc...) in java?
Asked
Active
Viewed 3.6k times
8
-
Your question is slightly vague. If you provide some reason for wanting a hash - such as to find out if a file has the same content as some other file - then the answer would be easier to give. In that case, you can use an MD5 hash (Message Digest). You already have answers for that. If you want a cryptographic hash (something that is not practical to forge) then you will not choose MD5. – Chris Mountford Feb 03 '11 at 07:15
-
1Given the question as is, `public int hash(File f) {return 0;}` is a nice solution. It's unclear what the file type has to do with it. First of all, a file is a sequence of bytes. – maaartinus Feb 03 '11 at 08:47
1 Answers
7
There is a related answer posted here Getting a File's MD5 Checksum in Java
You may look into "Real's How To" Here and use your desired hashing algorithm like this
String myHash = "MD5"; // or "SHA-1" or "SHA-256"
MessageDigest complete = MessageDigest.getInstance(myHash);