Naveen Gamage Naveen Gamage - 3 months ago 22
Linux Question

Calculating CRC32 checksum of a file

I'm trying to calculate CRC32 check sum of a file to use with this module Mod_zip, I tried to do this with

PHP
but unfortunately failed, even if passed won't be efficient for larger files.

I also tried
linux cksum
command but it calculates CRC checksum of the file.

I found that perl on linux can be used to calculate CRC32 of a file, if this is possible I could use shell_exec to import the output onto my
PHP
application, how can I do this?

Answer

Have you looked at Digest::CRC? From the documentation: "It contains wrapper functions with the correct parameters for CRC-CCITT, CRC-16, CRC-32 and CRC-64, as well as the CRC used in OpenPGP's ASCII-armored checksum."

use strict;
use warnings;
use Digest::CRC;

my $ctx = Digest::CRC->new( type => 'crc32' );

open my $fh, '<:raw', $ARGV[0] or die $!;
$ctx->addfile(*$fh);
close $fh;

print $ctx->hexdigest, "\n";

Command-line usage: perl script.pl inFile

Hope this helps!

Comments