BSD Checksum - Computation of The BSD Checksum

Computation of The BSD Checksum

Below is the relevant part of the GNU sum source code (GPL licensed). It computes a 16-bit checksum by adding up all 16-bit chars of the input data stream. In order to avoid many of the weaknesses of simply adding the data, the checksum accumulator is circular rotated to the right by one bit at each step before the new char is added.

FILE *fp; /* The file handle for input data* / int ch; /* Each character read. */ int checksum = 0; /* The checksum mod 2^16. */ while ((ch = getc (fp)) != EOF) { ... checksum = (checksum >> 1) + ((checksum & 1) << 15); checksum += ch; checksum &= 0xffff; /* Keep it within bounds. */ }

Below is a sample java code that calculates an 8-bit checksum. It adds each byte from the input byte array after a circular rotation of the checksum.

byte checksum(byte input) { byte checksum = 0; for (byte cur_byte: input) { checksum = (byte) (((checksum & 0xFF) >>> 1) + ((checksum & 0x1) << 7)); //rotate the accumulator checksum = (byte) ((checksum + cur_byte) & 0xFF); //add the next chunk } return checksum; }

Read more about this topic:  BSD Checksum

Famous quotes containing the words computation of the, computation of and/or computation:

    I suppose that Paderewski can play superbly, if not quite at his best, while his thoughts wander to the other end of the world, or possibly busy themselves with a computation of the receipts as he gazes out across the auditorium. I know a great actor, a master technician, can let his thoughts play truant from the scene ...
    Minnie Maddern Fiske (1865–1932)

    I suppose that Paderewski can play superbly, if not quite at his best, while his thoughts wander to the other end of the world, or possibly busy themselves with a computation of the receipts as he gazes out across the auditorium. I know a great actor, a master technician, can let his thoughts play truant from the scene ...
    Minnie Maddern Fiske (1865–1932)

    I suppose that Paderewski can play superbly, if not quite at his best, while his thoughts wander to the other end of the world, or possibly busy themselves with a computation of the receipts as he gazes out across the auditorium. I know a great actor, a master technician, can let his thoughts play truant from the scene ...
    Minnie Maddern Fiske (1865–1932)