Dear monks,
I came to problem, when I wanted convert python code to perl. It is about calculating custom hash of file, in general it should do following: filesize + 64bit chksum of the first and last 64k (even if they overlap because the file is smaller than 128k)
Source codes for other languages are
here
I am not good in bits operation, pack, unpack and uint64...but I did try, and here is what I came up with:
#!/usr/bin/perl -w
use strict;
die "need file as parameter" unless my $file = $ARGV[0];
open(my $fh, $file) or die "Can't open file $file: $!";
die "File $file is too small!" if -s $file < 65536;
my $hashstring = -s $file;
#calculate first 64kb
for(1 .. 65536/8) {
read($fh, my $byte, 4);
my $ll = unpack("LL", $byte);
#here is missing code
}
#calculate last 64kb
seek $fh, -65536, 2;
for(1 .. 65536/8) {
read($fh, my $byte, 4);
my $ll = unpack("LL", $byte);
#here is missing code
}
close($fh);
printf("Hash of $file is: %016x", $hashstring);
Please, help me, so I will get the same hash using perl. Thank you monkers!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.