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

I need to generate a checksum (64 bits) and have never done so before. First time for everything. In the Camel book, 2nd revision, page 237, there is sample code to generate a 32-bit checksum, (i think):
$checksum = unpack("%32C*", $foo) % 32767;
32767 is really just 2^(16-1)-1, but what is up with the first argument to unpack: "%32C*"? The 32C makes sense to me, but the % and the * elude me.

Second question is, will the following generate a 64-bit checksum:

$checksum = unpack("%64C*", $foo) % 2**(64-1)-1
? Also, if anybody knows of any good resources on generating checksums, please post them. Thanks in advance.

in the meantime, looks like a new Q&A is in order - How do I generate an N-bit checksum?

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

Replies are listed 'Best First'.
Re: On generating checksums and using unpack
by Albannach (Monsignor) on Apr 24, 2001 at 21:34 UTC
    While Digest::MD5 is great, I've used the unpack checksum plenty of times when I've had to test checksums that were already written in data (e.g. lots of data collection hardware use 16-bit checksums in their output). First, I think the 32-bit line should be:
    $checksum = unpack("%32C*", $foo) % 65535;

    The '%' is just telling unpack to use the special checksum function, I believe the 'C' means to break the input into unsigned chars (it's not for checksum) and the '*' means repeat the 'C' for the rest of the input. You'll want to look at pack for better info on the template syntax the two functions share. According to the manpage, %N works for N-bits, so I have no reason to doubt that your second snippet will do 64-bit checksums, but I can't verify that.

    --
    I'd like to be able to assign to an luser

Re: On generating checksums and using unpack
by dws (Chancellor) on Apr 24, 2001 at 21:17 UTC
    Have you disqualified all schemes based on Digest::MD5?

    Digest::MD5::md5_hex() returns a 32 byte string (representing 16 bytes/128 bits worth of hash). If you're required to have 64 bits of hash, you could take an MD5 hash and cut it in half.

    And MD5 has been vetted by experts. It's probably safer than rolling your own.