Matching Java md5 digests

Discussion in 'Perl Misc' started by douglasforrest@gmail.com, Feb 13, 2006.

  1. Guest

    I need to match md5 outputs being generated by a java app which in
    relevent part uses the following code:

    byte[] digest = md.digest();

    for (int i=0;i<digest.length;i++) {

    hexString.append(Integer.toHexString(digest & 0xFF));

    }

    return hexString.toString();

    where md is an instance of MessageDigest.getInstance("MD5");

    Using a particular value to be encoded, this function returns the
    31-digit hexedecimal string

    a1ee2c082ee66978aeca3d377fd11a5

    Using the perl Digest::MD5 md5_hex function on the same string returns
    thie following 32-digit hexidecimal string, which is the same as the
    java output with a leading "0" added.

    0a1ee2c082ee66978aeca3d377fd11a5

    I can't change the java so I am trying to to re-create the java results
    using perl. I've been using various permutations of unpack and sprintf
    on the 16-bit binary value generated by Digest::MD5 md5 without
    success so far.

    Any suggestions/pointers/ full-blown implementations would be much
    appreciated!
    , Feb 13, 2006
    #1
    1. Advertising

  2. wrote in
    news::

    > I need to match md5 outputs being generated by a java app which in
    > relevent part uses the following code:
    >
    > byte[] digest = md.digest();
    >
    > for (int i=0;i<digest.length;i++) {
    >
    > hexString.append(Integer.toHexString(digest & 0xFF));
    >
    > }
    >
    > return hexString.toString();
    >
    > where md is an instance of MessageDigest.getInstance("MD5");
    >
    > Using a particular value to be encoded, this function returns the
    > 31-digit hexedecimal string
    >
    > a1ee2c082ee66978aeca3d377fd11a5
    >
    > Using the perl Digest::MD5 md5_hex function on the same string returns
    > thie following 32-digit hexidecimal string, which is the same as the
    > java output with a leading "0" added.
    >
    > 0a1ee2c082ee66978aeca3d377fd11a5
    >
    > I can't change the java so I am trying to to re-create the java
    > results using perl. I've been using various permutations of unpack
    > and sprintf on the 16-bit binary value generated by Digest::MD5 md5
    > without success so far.


    I am not sure what 16-bit value you are talking about. MD5 produces a
    128-bit digest.

    I am not exactly sure what you are trying to do, but ...

    Are you just trying to remove leading zeros?

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $md5 = '0a1ee2c082ee66978aeca3d377fd11a5';
    $md5 =~ s{\A 0+ }{}x;
    print "$md5\n";

    __END__

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Feb 13, 2006
    #2
    1. Advertising

  3. Guest

    I meant 16-byte, not 16-bit; Digest::MD5 returns the 128-bits as a
    16-byte binary string.

    Unfortunately, the difference is not always a leading 0.

    What I need to do, I think, is to take each byte in a form to which I
    can bit-add 0xFF, convert the result to hex and concatenate the results
    into a string. The question is how?

    I tried unpacking using formats "i*" and "I*" into scalars and an array
    and then looping through, &'ing 0xFF to each value and then
    concatinating but the results are way off.
    , Feb 14, 2006
    #3
  4. Guest

    Solved!

    @temp = unpack("c*", $md5);

    for ($i=0; $i < scalar(@temp); $i++) {

    $result .= sprintf "%lx", $temp[$i] & 0xFF;
    }
    , Feb 14, 2006
    #4
  5. Dr.Ruud Guest

    douglasforrest:

    > I need to match md5 outputs being generated by a java app which in
    > relevent part uses the following code:
    >
    > for (int i=0;i<digest.length;i++) {
    > hexString.append(Integer.toHexString(digest & 0xFF));
    > }


    If that .toHexString() returns a single character for 0-15 (so not
    00-0F), then that code is asking for more collisions than md5 already
    does: for example the string 110 can come from 11-00 and from 01-10.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Feb 14, 2006
    #5
  6. Bart Lateur Guest

    wrote:

    >What I need to do, I think, is to take each byte in a form to which I
    >can bit-add 0xFF, convert the result to hex and concatenate the results
    >into a string. The question is how?


    bit-and. And that's pretty much a noop, at least in Perl, somebody
    probably added this to the Java code to convert signed integers to
    unsigned. Just ignore it, it cannot be the cause of the difference.

    --
    Bart.
    Bart Lateur, Feb 14, 2006
    #6
  7. Bart Lateur Guest

    wrote:

    >I can't change the java so I am trying to to re-create the java results
    >using perl.


    Wow. You'll keep using a buggy implementation and patch a bugfree
    implementation (AFAIK) to match its results. Way to go. Don't be
    surprised if you have to share data with other people and for them, the
    digest never matches.

    --
    Bart.
    Bart Lateur, Feb 14, 2006
    #7
  8. Guest

    Thanks everybody for all the comments and suggestions.

    A few final comments on my part:

    The java implementation is apparently not uncommon -- I found similar
    versions all over via Googling.

    As to matching the java program's results vs. changing the program, I
    have no choice: the java program is not within my control and changing
    it is not an option. The java program is being used by a company with
    over $1 billion in annual sales to generate access tokens to their data
    and to provide the access that my clients require, I have to match the
    tokens being generated by their java, additional collisions and all.

    The bit-and is exactly the cause of the differences.. My solution as
    written produces exactly the same results as the java.
    , Feb 14, 2006
    #8
    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. Igor Planinc

    Bit IO, digests, checksums

    Igor Planinc, Nov 15, 2005, in forum: Java
    Replies:
    15
    Views:
    672
    Andrey Kuznetsov
    Nov 18, 2005
  2. Cousin Stanley

    Debian MD5 Digests

    Cousin Stanley, Jul 27, 2004, in forum: Python
    Replies:
    0
    Views:
    533
    Cousin Stanley
    Jul 27, 2004
  3. Replies:
    9
    Views:
    17,094
    John Salerno
    May 8, 2006
  4. Niranjan
    Replies:
    2
    Views:
    243
    Niranjan
    Aug 28, 2008
  5. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    213
    Brian Candler
    Nov 21, 2008
Loading...

Share This Page