Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Digest::MD5 and SHA addfile appear to be clearing the buffer

by dt667 (Acolyte)
on Jun 10, 2014 at 17:26 UTC ( #1089432=perlquestion: print w/replies, xml ) Need Help??
dt667 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to figure out why the below code does not provide the correct SHA hashes. It appears the call to addfile($fh) is clearing out the buffer such that subsequent calls to different digests are only loading an empty buffer.

#! /usr/bin/perl use warnings; use strict; use Digest::MD5; use Digest::SHA; die "Usage: $0 file ..\n" unless @ARGV; foreach my $file (@ARGV) { my $fh; unless (open $fh, "<", $file) { warn "$0: open $file: $!"; next; } binmode($fh); my $md5 = Digest::MD5->new; my $sha1 = Digest::SHA->new(1); my $sha224 = Digest::SHA->new(224); my $sha256 = Digest::SHA->new(256); my $sha384 = Digest::SHA->new(384); my $sha512 = Digest::SHA->new(512); print "MD5: ", $md5->addfile($fh)->hexdigest, "\n"; print "SHA1: ", $sha1->addfile($fh)->hexdigest, "\n"; print "SHA224: ", $sha224->addfile($fh)->hexdigest, "\n"; print "SHA256: ", $sha256->addfile($fh)->hexdigest, "\n"; print "SHA384: ", $sha384->addfile($fh)->hexdigest, "\n"; print "SHA512: ", $sha512->addfile($fh)->hexdigest, "\n"; close $fh; } ###### #Expected Output: #MD5: 169bb71535030d6c432d5f6ab46d8b7e #SHA-1: e5997918cc7fe1920eb2995a39922e298875ee8a #SHA-224: aa275e2f5edd3102f8c2ade9bf12992118f18a83b53dc5c68370f5ad #SHA-256: 58e54ff89e5b920b08a40fa287fb28f69c3f2c8a5db7d41ed06ff424c33e +c92a #SHA-384: 2622d383b1d227385beb2aa3c94c65bd4d145bd50aba6ae9fbd30def10e8 +f937080ab1af277274156338ef500aaaf388 #SHA-512: d5646409891c4ac22f0e41e5aba899c4db58dca4c643eb2736f6b391c742 +ed7358bb770f4ea2823428443b60b166bc0827d361145268ea7589ff741efd0b1c5a ###### #Actual Output: #MD5: 169bb71535030d6c432d5f6ab46d8b7e #SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709 #SHA224: d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f #SHA256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b +855 #SHA384: 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e +1da274edebfe76f65fbd51ad2f14898b95b #SHA512: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce +9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

If I insert a call to open and binmode in between each print statement they work correctly, but I shouldn't have to open the file every time correct? This could be potentially time consuming if it is a very large file!!

Replies are listed 'Best First'.
Re: Digest::MD5 and SHA addfile appear to be clearing the buffer
by davido (Archbishop) on Jun 10, 2014 at 17:40 UTC

    The addfile($fh) tells Digest::SHA or Digest::MD5 to read from a filehandle. Once the handle has been read from in its entirety, it is "spent" or exhausted (it's at the end of the file). Subsequent reads aren't going to be successful unless you re-open the file, or rewind to the beginning with seek.

    Imagine this code:

    open my $fh, '<', 'infile'; while( <$fh> ) { # do something with $_. } my @remainder = <$fh>;

    That final read will be unsuccessful, because the while loop already exhausted the handle. The same behavior exists when you pass a handle to some other routine that exhausts it, which is what Digest::SHA is doing.


Re: Digest::MD5 and SHA addfile appear to be clearing the buffer
by atcroft (Abbot) on Jun 10, 2014 at 18:16 UTC

    Instead of using the filehandle, you could just pass the filename to the addfile() method (such as print "SHA224: ", $sha224->addfile($file, "b")->hexdigest, "\n";), which would let the module handle the details.

    Hope that helps.

      I just assumed the MD5 and SHA modules took the same parameters, but it appears the MD5 module requires a filehandle where as the SHA module takes either a filehandle or filename. Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1089432]
Approved by boftx
Front-paged by perlfan
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-08-16 15:34 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (168 votes). Check out past polls.