calculate MD5 checksum for a file

S

Steve

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/tomcat-5/v5.5.17/bin/apache-tomcat-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 & 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!!
 
A

alexandre_paterson

Steve said:
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 & 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 ;)
 
R

Red Orchid

Message-ID: said:
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.

I have downloaded the file from your link and executed your code
and got correct value as follows.

0bb2827c5eacf570b6064e24e0e6653b



And,
for (int i=0; i<b.length; i++)
{
String s = Integer.toString( ( b & 0xff ) + 0x100, 16 /* radix */
).substring( 1 );
System.out.print(s);
}



I think that if the size of a file is large, the following code
maybe be better than the above.

<code>
static final byte[] hex_char_table = {

(byte)'0', (byte)'1', (byte)'2', (byte)'3',
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b',
(byte)'c', (byte)'d', (byte)'e', (byte)'f'
};


String getHexString(byte[] raw) throws Exception {

byte[] hex = new byte[2 * raw.length];
int index = 0;

for (byte b : raw) {

int v = b & 0xFF;

hex[index++] = hex_char_table[v >>> 4];
hex[index++] = hex_char_table[v & 0xF];
}
return new String(hex, "ASCII");
}

</code>
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top