http://www.perlmonks.org?node_id=1089432

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!!