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

Re: Calculating the crc checksum of a file using perl?

by BrowserUk (Patriarch)
on Jul 09, 2011 at 17:37 UTC ( [id://913541]=note: print w/replies, xml ) Need Help??


in reply to Calculating the crc checksum of a file using perl?

If you are intending to use CRC32 to validate files against pre-existing checksums -- say those produced by a crc32 command for example -- then be sure to verify that whatever perl implementation you use matches. There are several (6) different polynomials used in various CRC32 standards.

If you are going to be using it to detect differences in files -- and you are producing all the checksums -- then I strongly advise using Adler32 rather than CRC32. The former has a much lower collision rate than the latter, and is faster to calculate.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Calculating the crc checksum of a file using perl?
by mrbark (Acolyte) on Mar 30, 2020 at 17:43 UTC
    Please keep in mind Adler is only good for LONG messages (large files). If you manipulate files of a few bytes it's a bad idea because most of the 32 bits will be unused.
Re^2: Calculating the crc checksum of a file using perl?
by AGhoulDoingPerl (Sexton) on Jul 10, 2011 at 03:50 UTC
    OK... Thank you for your tip
      Is there a more efficient way to get a checksum of all files in a directory?
      sub print_dir { my $dir_name = shift; print "DIR: $dir_name\n"; opendir ( my $dir_h , "$dir_name") or die "Unable to open dir :$dir +_name: $!\n"; while ( my $path = readdir($dir_h) ) { next if ($path eq "." or $path eq ".."); my $full_path = "$dir_name/$path"; if ( -l $full_path) { my $linkref = readlink($full_path); print " LINK: $full_path ==> $linkref\n" if $opt_debug; push(@bom_data, { Type => "symlink", Name => "$full_path", LinkR +ef => "$linkref" }); } elsif ( -d $full_path ) { push(@bom_data, { Type => "dir", Name => "$full_path" }); print_dir ( $full_path ); } else { my $ctx = Digest::Adler32::XS->new(); my $file_h = IO::File->new($full_path, "r"); die "? Error: Unable to open $full_path for read: $!" if (not de +fined($file_h)); binmode($file_h) || "? Error: Unable to set binmode on $file_h: +$!\n"; $ctx->addfile($file_h); $file_h->close; my $cksum = $ctx->hexdigest; print " FILE: $full_path ==> $cksum\n" if $opt_debug; push(@bom_data, { Type => "file", Name => "$full_path", Checksum + => "$cksum"}); } } close $dir_h; }
      Thanks for any suggestions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://913541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found