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

How to force Digest::SHA1/MD5 to reset?

by lestrrat (Deacon)
on Oct 21, 2003 at 21:20 UTC ( [id://301077]=perlquestion: print w/replies, xml ) Need Help??

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

I thought I knew how to use Digest::(MD5|SHA1), but apparently not. I've found out that when I have a function like this:

sub generate_signature { my $fh = shift; my $digest = Digest::SHA1->new(); $digest->addfile($fh); return $digest->sha1_hex(); }

I get different results for the first time I call the function and the subsequent calls in the same process. The sequence is always the same, though... and it looks something like this:

Signature for test.pl is bd7ce45573c74ca2373ddaf2f87fd7047bd1c6c7 Signature for test.pl is acdae4cd87ebecf0d2a4c96bec4282bb98adf831

But the two digests that I get are always the same, it's just that successive calls within the same process seem to stack the previous data passed by add() or addfile()

This leads me to believe that some how Digest::SHA1 is not resetting its state when new() is called.... I tried calling reset(), but that didn't work either.

Am I missing something? Is there a workaround?

Replies are listed 'Best First'.
Re: How to force Digest::SHA1/MD5 to reset?
by antirice (Priest) on Oct 21, 2003 at 21:44 UTC

    You are using a function on an object. In the documentation, you'll note that what you need is $digest->hexdigest; instead of $digest->sha1_hex;

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      D'oooooohhh!!!

      Thanks, that worked.... (bangs head against wall)

Re: How to force Digest::SHA1/MD5 to reset?
by sauoq (Abbot) on Oct 21, 2003 at 21:43 UTC
    But the two digests that I get are always the same, it's just that successive calls within the same process seem to stack the previous data passed by add() or addfile()

    Hence the word "add" in those function names...

    Is there a workaround?

    Create a new Digest object if you need a new Digest. (Or just use the functional interface rather than the OO interface.)

    Oops. My apologies... antirice is absolutely correct. You are creating a digest for the stringified Digest object. Something that probably looks like "Digest::SHA1=SCALAR(0x804c138)".

    -sauoq
    "My two cents aren't worth a dime.";
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found