Judging by the linked-to code, the hash is just summing the ASCII values of each byte. Translating this to fiarly idiomatic Perl, I come up with the following:

use strict; use warnings; use Fcntl ':seek'; use constant CHUNK => 65536; sub calc { my $file = shift or die "no filename given\n"; my $hash = -s $file; my $chunk = CHUNK; $hash < $chunk and die "$file is too small ($hash bytes < $chunk)\ +n"; open my $in, '<', $file or die "Cannot open $file for input: $!\n" +; local $/ = \$chunk; $hash += ord($_) for split //, readline($in); seek($in, SEEK_END, -$chunk); $hash += ord($_) for split //, readline($in); close $in; return sprintf '%016x', $hash; } print calc($_), "\t$_\n" for @ARGV;

...but, as GrandFather points out, without a test file and expected results, it's hard to know if there are any bugs in this.

update: I see you've added a test case. And I see that it's actually summing 32-bit values. The following code reflects that fact.

use strict; use warnings; use Fcntl ':seek'; use constant CHUNK => 65536; sub calc { my $file = shift or die "no filename given\n"; my $hash = -s $file; my $chunk = CHUNK; $hash < $chunk and die "$file is too small ($hash bytes < $chunk)\ +n"; open my $in, '<', $file or die "Cannot open $file for input: $!\n" +; local $/ = \$chunk; $hash += $_ for unpack 'N*' , readline($in); seek($in, SEEK_END, -$chunk); $hash += $_ for unpack 'N*' , readline($in); close $in; return sprintf '%016x', $hash; } my $test = 'test.deleteme'; open my $out, '>', $test; print $out ' ' x (65536*4); close $out; print calc($test), $/; unlink $test;

Hmm, but that doesn't match the test results. Back to the drawing board.

2nd update: Got it! It's summing quads, not longs, and clamping on overflow.

I had to use bigint, which is sub-optimal in that it gets the correct results at the cost of vastly increased execution time. Still, it does the job, and on a 64-bit CPU you should be able to remove the 'use bigint' and it will work just the same, and much, much faster.

use strict; use warnings; use Fcntl ':seek'; use bigint; use constant CHUNK => 65536; sub calc { my $file = shift or die "no filename given\n"; my $hash = -s $file; my $chunk = CHUNK; $hash < $chunk and die "$file is too small ($hash bytes < $chunk)\ +n"; open my $in, '<', $file or die "Cannot open $file for input: $!\n" +; local $/ = \$chunk; for my $quad (unpack 'q*' , readline($in)) { $hash += $quad; $hash &= 2 ** 64 - 1; } seek($in, SEEK_END, -$chunk); for my $quad (unpack 'q*' , readline($in)) { $hash += $quad; $hash &= 2 ** 64 - 1; } close $in; return sprintf '%016x', $hash; } open my $out, '>', 'test.deleteme'; print $out ' ' x (65536*4); close $out; print calc('test.deleteme'), $/;

• another intruder with the mooring in the heart of the Perl


In reply to Re: Creating custom hash of file by grinder
in thread Creating custom hash of file by 2ge

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.