Home > Archive > Java Security > May 2006 > calculate MD5 checksum for a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
calculate MD5 checksum for a file
|
|
|
| I want to write a java program to calculate MD5 checksum for a tomcat
file downloaded from apache's site:
http://www.apache.org/dist/tomcat/t...-5.5.17.exe.MD5
0bb2827c5eacf570b6064e24e0e6653b *apache-tomcat-5.5.17.exe
Here's the code I wrote, but I got the result
740171421a803f50dd573fd38b469a9b, which is different from the one in
tomcat's site. I am using the hex result.
byte[] b = createChecksum(args[0]);
for (int i=0; i<b.length; i++)
{
String s = Integer.toString( ( b[i] & 0xff ) + 0x100, 16 /* radix */
).substring( 1 );
System.out.print(s);
}
public static byte[] createChecksum(String filename) throws
Exception{
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
Any ideas why? please advise.
thanks!!
| |
| alexandre_paterson@yahoo.fr 2006-05-07, 7:24 pm |
| Steve wrote:
> I want to write a java program to calculate MD5 checksum for a tomcat
> file downloaded from apache's site:
....
> String s = Integer.toString( ( b[i] & 0xff ) + 0x100, 16 /* radix */
> ).substring( 1 );
it's kind of a hacky way to work around the fact that the leading
'0' isn't outputted but why not ;)
String as a "toHexString()" method btw.
> Any ideas why? please advise.
Because you're MD5-summing the wrong file? Because the
file got corrupted during transfer? Because a virus modified
the .exe? Because you overwrote your file with another one?
Seriously, I downloaded the file and tried your code and it
gives the correct checksum on my computer.
What does md5sum, when invoked from the "shell", say?
For example, on my system it says:
[user ~/] 1 $ md5sum /home/public/dl/apache-tomcat-5.5.17.exe
0bb2827c5eacf570b6064e24e0e6653b
/home/public/dl/apache-tomcat-5.5.17.exe
Which means that when running the program, I can probably
expect the program to be faulty (or faulty called) if it doesn't give
the same answer. (here both the Un*x md5sum command
and your program give the same answer as the one given by the
file apache-tomcat-5.5.17.exe.MD5).
(and there are various ways to get the MD5 checksum of a file under
Windows)
After checking on Google, by entering the MD5sum your program
outputted, it looks like you're computing the MD5sum of a program
called fsum.exe...
So you're not calling your program with the correct argument (or
you overwrote apache-tomcat-5.5.17.exe with fsum.exe).
Hope it helps ;)
| |
| Roedy Green 2006-05-07, 7:24 pm |
| On 6 May 2006 10:20:36 -0700, "Steve" <javacc2@gmail.com> wrote,
quoted or indirectly quoted someone who said :
>I want to write a java program to calculate MD5 checksum for a tomcat
>file downloaded from apache's site:
I don't see anything the matter with your code. You might single step
it to look for anomalies. You might put a count in your code to make
sure you are getting the same number of bytes.
You might also try some other download, different site and see if your
MD5 for that matches. It could be the site has an out of date
checksum.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|