Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Digest::MD5 Bug?

by Tuna (Friar)
on Aug 15, 2001 at 21:01 UTC ( [id://105099]=perlquestion: print w/replies, xml ) Need Help??

Tuna has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone explain this behavior?

#!/usr/bin/perl -w use strict; use Digest::MD5; my %local; my $file = shift || "/etc/passwd"; open(FILE, $file) or die "Can't open '$file': $!"; binmode(FILE); my $md5 = Digest::MD5->new; while (<FILE>) { $md5->add($_); } close FILE; $local{$file} = $md5->b64digest; foreach my $k(keys %local) { print "Key = $k\nValue = $local{$k}\n\n"; } print $md5->b64digest, " $file\n";

The above prints:

Key = /etc/passwd Value = Z+9+EFlyq2r1T90U9TiCiA 1B2M2Y8AsgTpgAmY7PhCfg /etc/passwd

Why would the fingerprint values be different?

Replies are listed 'Best First'.
(ichimunki) Re: Digest::MD5 Bug?
by ichimunki (Priest) on Aug 15, 2001 at 21:16 UTC
    from the perldoc:
    $md5->digest

    Return the binary digest for the message. Note that the digest operation is effectively a destructive, read-once operation. Once it has been performed, the Digest::MD5 object is automatically reset and can be used to calculate another digest value.

    $md5->b64digest

    Same as $md5->digest, but will return the digest as a base64 encoded string.

    You will have to run the whole file in again if you want to compute the fingerprint a second time.

Re: Digest::MD5 Bug?
by Hofmator (Curate) on Aug 15, 2001 at 21:21 UTC

    RTFM ;-) it's clearly written in the perldoc

    Note that the "digest" operation is effectively a destructive, read-once operation. Once it has been performed, the "Digest::MD5" object is automatically "reset" and can be used to calculate another digest value.
    so no surprise there as you are calling the method twice.

    Update:Sorry for the duplicaton there, I was too slow ;-)

    -- Hofmator

Log In?
Username:
Password:

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

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

    No recent polls found