Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.4k points)

I am looking to use Java to get the MD5 checksum of a file. I was really surprised but I haven't been able to find anything that shows how to get the MD5 checksum of a file.

How is it done?

1 Answer

0 votes
by (46k points)

You can try it's an input stream decorator so that you can calculate the digest while using the input stream as you usually would, instead of having to get an additional pass over the data.

Here's the Syntax:

MessageDigest md = MessageDigest.getInstance("MD5");

try (InputStream is = Files.newInputStream(Paths.get("file.txt"));

     DigestInputStream dis = new DigestInputStream(is, md)) 

byte[] digest = md.digest();

Related questions

0 votes
2 answers
asked Jul 12, 2019 in Java by Aditya98 (1.3k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories