calculate MD5 checksum for a file

Discussion in 'Java' started by Steve, May 6, 2006.

  1. Steve

    Steve Guest

    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!!
    Steve, May 6, 2006
    #1
    1. Advertising

  2. Steve

    Guest

    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 & 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 ;)
    , May 6, 2006
    #2
    1. Advertising

  3. Steve

    Red Orchid Guest

    "Steve" <> wrote or quoted in
    Message-ID: <>:

    > 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>
    Red Orchid, May 6, 2006
    #3
  4. Steve

    Roedy Green Guest

    On 6 May 2006 10:20:36 -0700, "Steve" <> 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.
    Roedy Green, May 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abby
    Replies:
    4
    Views:
    18,875
  2. Mercuro

    Shorter checksum than MD5

    Mercuro, Sep 9, 2004, in forum: Python
    Replies:
    17
    Views:
    1,229
    Tim Peters
    Sep 11, 2004
  3. Replies:
    9
    Views:
    17,066
    John Salerno
    May 8, 2006
  4. kkwweett

    wrong md5 checksum

    kkwweett, Jul 1, 2008, in forum: Python
    Replies:
    3
    Views:
    352
    Terry Reedy
    Jul 3, 2008
  5. Doug Jolley

    MD5 Checksum of a String

    Doug Jolley, Jun 23, 2010, in forum: Ruby
    Replies:
    2
    Views:
    113
    Doug Jolley
    Jun 23, 2010
Loading...

Share This Page