Developer community
0 votes
3.9k views
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?
asked by Guru (24.6k points) 31 57 92 | 3.9k views

1 Answer

0 votes
 
There's an input stream decorator, java.security.DigestInputStream, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data.
 
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"))) {
  DigestInputStream dis = new DigestInputStream(is, md);
  /* Read stream to EOF as normal... */
}
byte[] digest = md.digest();
 
 
answered by Guru (27.1k points) 35 52 112

Related questions

0 votes
2 answers
asked by john Guru (27.1k points) 35 52 112 | 21 views
0 votes
1 answer
asked by sara Guru (24.6k points) 31 57 92 | 43 views
0 votes
1 answer
asked by keem Expert (13.2k points) 29 44 62 | 15 views
0 votes
1 answer
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,754 questions
4,046 answers
117 comments
10,709 users