Debugging exercise

Here is how I would tackle the problem.

Finding a suspect.

I can see that the output of Digest::MD5 in OO mode is incorrect.

By comparing with some independent programs, you realize that the output of md5sum and mysql is the same as the functional interface of Digest::MD5

$ perl -e 'use Digest::MD5; print Digest::MD5->new->md5_hex("foobarbaz +") ,$/' e05e07ceb87ddb19ccba8a51a57ac120 $ perl -e 'use Digest::MD5 qw(md5_hex); print md5_hex("foobarbaz"),$/' 6df23dc03f9b54cc38a0fc1483df6e21 $ echo -n foobarbaz | md5sum 6df23dc03f9b54cc38a0fc1483df6e21 *- $ mysql -e "select md5('foobarbaz')" +----------------------------------+ | md5('foobarbaz') | +----------------------------------+ | 6df23dc03f9b54cc38a0fc1483df6e21 | +----------------------------------+

Getting the evidence

Now, let's apply a constitutional principle, according tro which, everybody is innocent until proved guilty. So I would say that the OO interface is innocent, and look at the docs. They say that this is the regular format of the OO interface.

use Digest::MD5; $md5 = Digest::MD5->new; $md5->add('foo', 'bar'); $md5->add('baz'); $digest = $md5->hexdigest;

This is different from what you have done. Let's try it the "official" way.

sub create_checksum { my $self = shift; my $data = shift; my $foo = $$data; my $ctx = Digest::MD5->new; $ctx->add($foo); my $cs = $ctx->hexdigest(); return $cs; }

This works fine. Proof of concept:

$ perl -e 'use Digest::MD5; my $x= Digest::MD5->new; $x->add("foobarba +z"); print $x->hexdigest,$/' 6df23dc03f9b54cc38a0fc1483df6e21

The verdict

The OO interface is innocent.

Your implementation is guilty. :)

 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Re: MD5 Peculiarities by gmax
in thread MD5 Peculiarities by skazat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.