0

There are several options (e.g. here) to calculate the md5 hash over a file. However, it seems all require reading in the file first. Is there any way to achieve that without reading the full file into memory?

Kavau
  • 519
  • 2
  • 7
  • 21
  • As others mentioned it's not possible. But why do you want to use MD5 anyway? Are you aware that MD5 is not safe to use anymore? (See https://en.wikipedia.org/wiki/MD5#Collision_vulnerabilities) – Joachim Rohde Dec 06 '19 at 10:19
  • *"However, it seems all require reading in the file first."* They require reading the file, yes. *"Is there any way to achieve that without reading the full file into memory?"* Yes, there's no reason you have to read the entire file into memory to do a checksum. See [`MessageDigest`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/security/MessageDigest.html) and the answers to the [linked question](https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java). – T.J. Crowder Dec 06 '19 at 10:20

1 Answers1

0

Obviously for the hash function to calculate the correct hash value, you need to read all bytes of the file. But you do not have to read them to memory all at once. You can use a stream like that:

MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"));
     DigestInputStream dis = new DigestInputStream(is, md)) 
{
  /* Read decorated stream (dis) to EOF as normal... */
}
byte[] digest = md.digest();

Source

SteffenJacobs
  • 402
  • 2
  • 10
  • Actually, the question the answer you linked above is using makes this question a duplicate. When an answer is **directly** applicable like that, please comment pointing out the duplicate rather than posting an answer. – T.J. Crowder Dec 06 '19 at 10:18