Re: Opinions on 16-bit checksums.

From: Hans-Bernhard Broeker (broeker_at_physik.rwth-aachen.de)
Date: 08/25/04


Date: 25 Aug 2004 07:17:11 GMT

Kermit <Kermit333@hotmail.com> wrote:

> Since I see that Endianess is a current topic, I'd like to ask a question
> concerning it and calculating 16-bit checksums. I've never seen a standard
> anywhere that describes exactly what a checksum is.

What made you believe that the community of programmers would ever
have agreed on a single "standard" way of doing it in the first place?

> However, I have mainly seen them calculated in one of three ways.

> 1) Simply calculate a 32-bit value which is the sum of each byte
> added to a running sum. At the end, cast this 32-bit value to a
> 16-bit value, and call it the checksum.

I strongly hope nobody actually ever does it that way, on anything
smaller than a 32-bit platform, since that would be quite silly.
Calculating in 32 bits is wasteful if you're only going to be using
the lower 16 bits of the result --- just let a 16-bit sum roll over
and be done with it.

Anyway, this checksumming method is highly dubious, to put it mildly.
There are just too many types of rather likely errors that it won't
ever catch. You don't want a checksum's core operation to be
commutative.

> 2) Calculate the same way as above, but pull two bytes of the file
> out at a time such that you are adding them as 16-bit values in one
> of the following ways.

> a) Little-endian
> b) Big-endian.

Endianness doesn't really enter the figure as an option you would have
to think about. Just interpret pairs of bytes as 16-bit values in
whatever endianness your platform prefers, and add them.

E.g. note that both 2-a and 2-b would write the same checksum, if seen
as bytes:

        0x09 0x06

> I've always preferred to calculate it 16-bits at a time, because that way,
> you can detect byte-reversal issues.

But that only makes a difference if reversal of pairs of bytes at at
odd distances (1, 3 or 1023 bytes apart) is more important to detect /
more likely to happen than reversal of pairs an even distance apart
(2, 4, or 1024, e.g.). That assumption seems unwarranted.

-- 
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.


Relevant Pages

  • Re: Editors
    ... all you have is a standard PE. ... RosAsm produced PEs are ... Standard PEs. ... > checksum and verifying with the value from original PE. ...
    (alt.lang.asm)
  • hexadecimal calculating: turning on the high bit
    ... I have hardly any experience with hexadecimal calculating and 'and' ... cHex: string; ... iChecksum: integer; ... I find that the only example that doesnt compute right the checksum ...
    (alt.comp.lang.borland-delphi)
  • Re: How Extract The Fedorecore iso cd
    ... Checksum is calculated before burning the CD to make sure the ... For calculating the sum, follow the instructions here ... the images are good and you can burn them to CDs using Nero. ... If you are downloading the DVD image, the CD images are not required. ...
    (Fedora)
  • Opinions on 16-bit checksums.
    ... Since I see that Endianess is a current topic, I'd like to ask a question ... concerning it and calculating 16-bit checksums. ... anywhere that describes exactly what a checksum is. ... Algorithm 2-a would yield: ...
    (comp.arch.embedded)
  • Re: ReadFile failed for a file in release directory WM6.1
    ... That will display the file (whose checksum is calculated) and the ... The file actually is a driver dll that I need to read. ... of WM 6.1 that they are calculating the hash for any particular driver ...
    (microsoft.public.windowsce.platbuilder)