# Checksum
## What is a Checksum
A checksum is a simple error detection technique that produces a fixed-size value derived from the data being checked. The sender calculates the checksum and appends it to the data. The receiver recalculates the checksum from the received data and compares it to the received checksum value. If the two checksums are different, the data was corrupted during transmission. Checksums are simpler and faster to compute than CRC but provide weaker error detection.
## One's Complement Checksum
The Internet Checksum used in IP, TCP, and UDP headers uses one's complement arithmetic. The data is divided into 16-bit words. All these words are added together using one's complement addition, where any carry out of the 16-bit result is added back to the least significant bit. The one's complement of the sum, obtained by inverting all bits, is the checksum. The receiver adds all the 16-bit words of the data including the checksum. If the result is all 1 bits (the one's complement of zero), no error is detected.
## Checksum in TCP
The TCP checksum covers the TCP header, the TCP data, and a pseudo-header containing the source IP address, destination IP address, protocol number, and TCP length. Including the IP addresses in the checksum calculation ensures that if a packet is misdelivered to the wrong host, the TCP checksum will fail and the packet will be discarded.
## Checksum in UDP
The UDP checksum is optional in IPv4 but mandatory in IPv6. It covers the UDP header, the UDP data, and the same pseudo-header as TCP. If a UDP sender does not calculate a checksum, it sets the checksum field to zero, indicating that no checksum is provided.
## Limitations of Checksum
The one's complement checksum has weaknesses. It cannot detect errors where two words are swapped in order, because addition is commutative and the sum is the same regardless of order. It has a relatively high probability of missing certain patterns of multiple bit errors. For these reasons, CRC provides stronger error detection than the checksum. However, checksum is faster to compute and sufficient for many purposes.Back to Course