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


In reply to Digest::MD5 and SHA addfile appear to be clearing the buffer by dt667

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.