Fletcher's checksum

Fletcher's checksum

Fletcher's checksum is one of several types of checksum algorithms, which are relatively simple processes used by computers to check the integrity of data.

The implementation of the Fletcher-32 is very similar to the Adler-32 algorithm but several differences should be noted. Fletcher wraps around at modulo 65535 while Adler wraps at the prime 65521. In other words, Fletcher adds overflow bits (16-31) into its sum; while Adler multiplies those bits by 15, then adds the product into its sum. Fletcher-32 works on 16 bit data while Adler works on 8 bit data.

It is designed to overcome some of the inadequacies of simply summing all the bytes as in the original checksum.Fletcher's checksum, unlike the original checksum, can detect the inserting/deleting of zero value bytes, the reordering of bytes, and the incrementing and decrementing of bytes in opposite directions.

Fletcher's checksum is described in RFC 1146. You can also find information about generating (as well as verifying) such a checksum in Annex B of RFC 905.

Fletcher-32 is slightly more reliable than Adler-32 [ [http://www.zlib.net/maxino06_fletcher-adler.pdf Revisiting Fletcher and Adler Checksums] ]

An optimized implementation in the C programming language operates as follows:

uint32_t fletcher32( uint16_t *data, size_t len ){ uint32_t sum1 = 0xffff, sum2 = 0xffff; while (len) { unsigned tlen = len > 360 ? 360 : len; len -= tlen; do { sum1 += *data++; sum2 += sum1; } while (--tlen); sum1 = (sum1 & 0xffff) + (sum1 >> 16); sum2 = (sum2 & 0xffff) + (sum2 >> 16); } /* Second reduction step to reduce sums to 16 bits */ sum1 = (sum1 & 0xffff) + (sum1 >> 16); sum2 = (sum2 & 0xffff) + (sum2 >> 16); return sum2 << 16 | sum1;}A few tricks, well-known to implementors of the IP checksum, are used here for efficiency:
* This reduces to the range 1..65535 rather than 0..65534. Modulo 65535, the values 65535 = 0xffff and 0 are equivalent, but it is easier to detect overflow if the former convention is used. This also provides the guarantee that the resultant checksum will never be zero, so that value is available for a special flag, such as "checksum not yet computed".
* 65536 ≡ 1 mod 65535, so the end-around carry expression (x & 0xffff) + (x >> 16) reduces x modulo 65535. Only doing it once is not guaranteed to be complete, but it will be less than 0x1fffe. A second repetition guarantees a fully reduced sum in the range of 1..65535.
* This uses a 32-bit accumulator to perform a number of sums before doing any modular reduction. The magic value 360 is the largest number of sums that can be performed without numeric overflow. Any smaller value is also permissible; 256 may be convenient in many cases.
* For 8 bit checksums (with 16 bit accumulators) the maximum number of sums that can be performed before doing the modular reduction is 21.


An efficient 8 bit implementation in the C programming language is as follows:

void fletcher16( uint8_t *checkA, uint8_t *checkB, uint8_t *data, size_t len ){ uint16_t sum1 = 0xff, sum2 = 0xff; while (len) { size_t tlen = len > 21 ? 21 : len; len -= tlen; do { sum1 += *data++; sum2 += sum1; } while (--tlen); sum1 = (sum1 & 0xff) + (sum1 >> 8); sum2 = (sum2 & 0xff) + (sum2 >> 8); } /* Second reduction step to reduce sums to 8 bits */ sum1 = (sum1 & 0xff) + (sum1 >> 8); sum2 = (sum2 & 0xff) + (sum2 >> 8); *checkA = (uint8_t)sum1; *checkB = (uint8_t)sum2; return;}

References

* Fletcher, J. G., “An Arithmetic Checksum for Serial Transmissions”, IEEE Trans. on Comm., Vol. COM-30, No. 1, January 1982, pp 247-252.

External links

*RFC 1146 - "TCP Alternate Checksum Options" describes the Fletcher checksum algorithm.
* [http://citeseer.ist.psu.edu/34744.html Performance of Checksums and CRCs over Real Data]
* [http://ieeexplore.ieee.org/iel5/8858/4358699/04358707.pdf?arnumber=4358707 Maxino & Koopman] - compares Adler, Fletcher, and CRC checksums


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Fletcher — may refer to one of the following:Ideas and companies* A fletcher makes arrows, see fletching. * The Fletcher School of Law and Diplomacy, the graduate school of international relations of Tufts University, located in Medford, Massachusetts *… …   Wikipedia

  • Checksum — Effect of a typical checksum function (the Unix cksum utility). A checksum or hash sum is a fixed size datum computed from an arbitrary block of digital data for the purpose of detecting accidental errors that may have been introduced during its… …   Wikipedia

  • Adler-32 — is a checksum algorithm which was invented by Mark Adler. Compared to a cyclic redundancy check of the same length it trades reliability for speed. Compared to its original form (Fletcher 16), Adler 32 is more reliable than Fletcher 16. However,… …   Wikipedia

  • Cyclic redundancy check — A cyclic redundancy check (CRC) is an error detecting code designed to detect accidental changes to raw computer data, and is commonly used in digital networks and storage devices such as hard disk drives. Blocks of data entering these systems… …   Wikipedia

  • Adler-32 — Adler 32  хеш функция, разработанная Марком Адлером (англ.). Является модификацией контрольной суммы Fletcher (англ.). Вычисляет значение контрольной суммы в соответствии с RFC 1950 для массива байт или его фрагмента. Данный… …   Википедия

  • Rsync — infobox software name = rsync caption = rsync logo author = Andrew Tridgell, Paul Mackerras developer = Wayne Davison latest release version = 3.0.4 latest release date = September 6 2008 genre = Data transfer/ differential backup license = GNU… …   Wikipedia

  • rsync — Original author(s) Andrew Tridgell, Paul Mackerras Developer(s) Wayne Davison Initia …   Wikipedia

  • Computation of CRC — Computation of a cyclic redundancy check is derived from the mathematics of polynomial division, modulo two. In practice, it resembles long division of the binary message string, with a fixed number of zeroes appended, by the generator polynomial …   Wikipedia

  • Mathematics of CRC — Cyclic Redundancy Check (CRC) is based on division in the ring of polynomials over the finite field GF(2) (the integers modulo 2), that is, the set of polynomials where each coefficient is either zero or one, and arithmetic operations wrap around …   Wikipedia

  • Циклический избыточный код — Эта статья  о коде. О методе мозгового штурма см. CRC карта. Циклический избыточный код (англ. Cyclic redundancy check, CRC[1])  алгоритм вычисления контрольной суммы, предназначенный для проверки целостности… …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”