Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: MD5 Peculiarities

by gmax (Abbot)
on Jun 22, 2003 at 10:11 UTC ( [id://267937]=note: print w/replies, xml ) Need Help??


in reply to MD5 Peculiarities

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. :)

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

Replies are listed 'Best First'.
Re: Re: MD5 Peculiarities
by isotope (Deacon) on Jun 22, 2003 at 15:03 UTC
    Excellent investigation, gmax. In fact, the problem is that md5_hex is purely a standalone function, never a method. By calling it as a method, the original poster has been calculating the MD5 checksum of the scalar representation of the object $ctx, likely something similar to Digest::MD5=SCALAR(0x804c340), which of course will be different each time.

    --isotope
Re: Re: MD5 Peculiarities
by skazat (Chaplain) on Jun 23, 2003 at 04:53 UTC

    gmax,

    It is over and above your monastery duties to post this and I have to tell you that I am grateful. Thanks for pointing out the woes of my ways. I am one step closer to enlightenment.

    Is it neglectful for me to think that the OO interface is exactly reflective of the Functional interface? In this case, yes.

     

    -justin simoni
    !skazat!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://267937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found