rupesh has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,
Some of you might be familiar with the cksum(or checksum)utility in Unix which does a bit-by-bit comparison of files and gives out a number, which is never differrent if there are the slightest changes in the files. I was wondering if there is such a utility for Windows, and if not can one be made in perl?

Thanks...


we're born with our eyes closed and our mouths wide open, and we spend our entire life trying to rectify that mistake of nature. - anonymous.

Replies are listed 'Best First'.
Re: Checksum for Windows
by tachyon (Chancellor) on Jul 21, 2003 at 13:08 UTC

    System independent.....

    #!/usr/bin/perl use Digest::MD5 'md5_hex'; undef $/; open F, $ARGV[0] or die "Usage $0 <file>\n$!\n"; binmode F; print md5_hex(<F>); close F;

    Update

    Added binmode for Win32, Mac as suggested by particle

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      although i haven't tested, this looks suspiciously like a case where binmode might be necessary.

      as it stands, it can also be written as a one-liner, like

      perl -000 -MDigest::MD5=md5_hex -ne"$\=md5_hex}{print" your_file_here

      note the triple-zero option to undefine the output line seperator.

      ~Particle *accelerates*

Re: Checksum for Windows
by Hofmator (Curate) on Jul 21, 2003 at 13:08 UTC
    the cksum(or checksum)utility in Unix which does a bit-by-bit comparison of files and gives out a number, which is never differrent if there are the slightest changes in the files
    Well, this is not exactly what cksum does. It doesn't do any comparing of files. It calculates a number based on the input file and it is very unlikely - but not impossible - that this number is the same for a file and a slightly changed version of that file.

    Concerning any perl implementations, search at http://search.cpan.org for Checksum or Digest and you will find plenty of solutions.

    -- Hofmator

Re: Checksum for Windows
by BUU (Prior) on Jul 21, 2003 at 12:40 UTC
Re: Checksum for Windows
by flounder99 (Friar) on Jul 21, 2003 at 16:22 UTC
    Staight from perldoc -f unpack
      For example, the following computes the same number as the System V sum program:
    
    $checksum = do { local $/; # slurp! unpack("%32C*",<>) % 65535; };

    --

    flounder

      I actually tried the perldoc unpack checksum script on my mac, and it didn't work...

      Mac users need to be aware that MacOS uses a different (better) algorithm for their sum(1) command. If you want to use the old algorithm, you have to type in the following command:

      cksum -o2 filename