Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

calculate MD5 checksum for a file

Thread view: 
Steve - 06 May 2006 18:20 GMT
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[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 - 06 May 2006 19:25 GMT
> 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 ;)
Red Orchid - 06 May 2006 20:14 GMT
"Steve" <javacc2@gmail.com> wrote or quoted in
Message-ID: <1146936036.084827.193400@u72g2000cwu.googlegroups.com>:

> 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[i] & 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>
Roedy Green - 07 May 2006 00:41 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.