CRCs are based on division in the ring of polynomials over the integers modulo 2. A string of bits is interpreted as the coefficients of a polynomial of this sort, and to find the CRC, we divide it by another fixed polynomial. The coefficients of the remainder polynomial are the CRC. (See more details in http://www.repairfaq...K/F_crc_v3.html.) The IEEE 802.3 standard specifies the polynomial and the padding of the data (with its length represented in 4 bytes).
There are several CRC variants in use:
CRC-8 x^8 + x^2 + x^ + 1
CRC-CCITT x^16 + x^12 + x^5 + 1
CRC-16 (IBM) x^16 +x^15 + x^2 + 1
CRC-32 (802.3) x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^ + 1
CRC32c (Castagnoli) x^32 + x^28 + x^27 + x^26 + x^25 + x^23 + x^22 + x^20 + x^19 + x^18 + x^14 + x^13 + x^11 + x^10 + x^9 + x^8 + x^6 + 1
Here is a simple script for CRC-32, the most often used CRC version. When using it the for a (binary) file, the data has to be converted to a string of hex digits, e.g. with this script, also used here. For large files the input has to be modified, so pieces of data are read, without closing and reopening the file. Also, the code can be optimized with using tables and handling several bits together, but this script could still serve as a verifier for the accelerated code.
SetFormat Integer, H x = 00000000000000000000000000000000000000000000000000000000000000000000000000000000 ; 864d7f99 MsgBox % CRC32(x) x = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ; c55e457a MsgBox % CRC32(x) x = 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728 ; bf671ed0 MsgBox % CRC32(x) Return CRC32(x) { L := StrLen(x)>>1 ; length in bytes StringTrimLeft L, L, 2 ; remove leading 0x L = 0000000%L% StringRight L, L, 8 ; 8 hex digits x = %x%%L% ; standard pad R = 0xFFFFFFFF ; initial register value Loop Parse, x { y := "0x" A_LoopField ; one hex digit at a time Loop 4 { R := (R << 1) ^ ( (y << (A_Index+28)) & 0x100000000) IfGreater R,0xFFFFFFFF R := R ^ 0x104C11DB7 } } Return ~R ; ones complement is the CRC }