Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found