1. Home

A Comprehensive Guide on Software Key Tutorials

Elevate your software skills with our comprehensive tutorials covering concepts from basic to advanced techniques. Dive in and become a software pro!

• 220 Lessons
• 37 Hours
209

# Checksum

Updated on 19/07/20241,905 Views

## Introduction

Checksum in computer networks is used to ensure the integrity of data transmitted from the sender to the receiver. It involves performing a mathematical calculation on the data, which generates a checksum value. This checksum value is appended to the end of the original data unit before transmission.

## Overview

Here's a summary of the error detection code - checksum and its algorithmic process.

• Data Division: The data to be transmitted is divided into equal subunits of a fixed bit length, typically 16 bits.
• Checksum Generation: The checksum generator on the sender side adds all the subunits together using a one's complement method. This addition results in a sum of n bits.
• Complementation: The resultant sum is then complemented, meaning that all the bits are flipped (ones become zeros and vice versa). This complemented sum becomes the checksum.
• Transmission: The original data unit is combined with the checksum, and the entire package is transmitted to the receiver.
• Checksum Checking: Upon receiving the data + checksum, the receiver passes it to the checksum checker.
• Checksum Verification: The checksum checker divides the received data unit into equal subunits, including the appended checksum.
• Checksum Calculation: The receiver's checksum checker adds all these subunits together using the same one's complement method as the sender.
• Complementation: The sum obtained by the receiver is then complemented.
• Error Detection: If the complemented result is zero, it means the data is error-free. Then, the receiver accepts the data. However, if the result is non-zero, it indicates that an error occurred during transmission. Now, the receiver rejects the data.

## What Is Checksum?

Checksum in computer networks are applied to ensure data integrity. When data is sent from a sender to a receiver, it may be susceptible to errors due to noise, interference, or other issues in the communication channel. The checksum acts as a simple and efficient error detection method to identify such errors.

## Why Apply Checksum?

By generating a checksum from the data and appending it to the transmitted message, the receiver can independently calculate the checksum upon receiving the data. If the received checksum matches the calculated checksum, it indicates that the data was likely transmitted correctly without any errors.

In case the calculated checksum at the receiver's end differs from the received checksum, it signifies that errors have occurred during transmission. In such cases, the receiver can request the sender to retransmit the data or take other appropriate actions to ensure accurate data delivery. Applying checksum helps in maintaining data integrity and improving the overall reliability of data transmission in networks.

## Working Steps for Checksum

Checksum is a mathematical algorithm used to verify data integrity and detect errors in digital data. It is commonly used in data transmission and storage to ensure that the data received or stored matches the original data. Here are the steps to calculate a simple checksum for a given set of data:

Data Representation: The data to be protected by the checksum is represented in a binary format. This means each character or byte in the data is converted to its corresponding binary representation.

Split Data: Divide the data into fixed-size segments or blocks. The size of these segments can vary depending on the specific checksum algorithm being used.

Checksum Algorithm: Choose a checksum algorithm suitable for your application. Commonly used algorithms include CRC (Cyclic Redundancy Check), Adler-32, and Fletcher's checksum, among others.

Checksum Calculation: Apply the chosen checksum algorithm to each segment of the data. The algorithm will perform calculations on the binary values of the data in the segment and produce a checksum value.

Checksum Concatenation: Concatenate the individual checksums of all segments to create the final checksum for the entire data.

Transmit or Store the Data: Send or store the original data along with the calculated checksum.

Data Reception or Retrieval: When receiving or retrieving the data, the recipient or reader will also calculate the checksum of the received or retrieved data using the same algorithm.

Compare Checksums: Compare the calculated checksum with the received or retrieved checksum. If the two checksums match, it indicates that the data is likely to be error-free and has been transmitted or stored correctly. If the checksums differ, it suggests that errors have occurred during data transmission or storage.

Error Handling: In case of a checksum mismatch, the recipient or reader can request a retransmission of the data or take other appropriate error correction measures.

It's important to note that the specific steps and algorithms used for checksum computation can vary depending on the application and requirements. Some algorithms are more robust and provide better error-detection capabilities than others. The choice of algorithm and the size of the checksum can impact the efficiency and reliability of the error detection process.

Here is an example of using the checksum function:

Code:

`def calculate_checksum(data):    checksum = 0    for byte in data:        checksum ^= byte    return checksumdef main():    # Sample data represented as a list of bytes (you can modify this as per your data)    data = [0x01, 0x23, 0x45, 0x67, 0x89]    # Calculate the checksum    checksum = calculate_checksum(data)    # Print the result    print("Data:", data)    print("Calculated Checksum:", hex(checksum))if __name__ == "__main__":    main()`

In this code, the calculate_checksum function takes a list of bytes as input and returns the checksum calculated using the XOR operation. The main function demonstrates how to use the calculate_checksum function with sample data. You can modify the data list to represent your own data.

Remember that this is just a simple example using the XOR operation. In practical applications, you might want to use more sophisticated checksum algorithms like CRC or Adler-32, depending on your specific needs. Additionally, the way you represent and interpret the data will vary based on your use case (e.g., ASCII, UTF-8, binary data, etc.).

Explanation:

1. calculate_checksum function:
• This function takes a list of bytes named data as input.
• It initialises a variable checksum to 0, which will be used to store the calculated checksum.
• The function then iterates through each byte in the data list using a for loop.
• For each byte in the data, it performs a bitwise XOR operation with the current value of checksum.
• The result of the XOR operation is stored back in the checksum variable, updating it with the new value.
• Once all the bytes in data are processed, the function returns the final calculated checksum.
1. main function:
• The main function is where the program execution starts.
• It defines a sample data list data, represented as a list of bytes in hexadecimal format.
• It then calls the calculate_checksum function, passing the data list as an argument, and stores the result in the checksum variable.
• After calculating the checksum, it prints both the original data and the calculated checksum in hexadecimal format.
1. if __name__ == "__main__": block:
• This block ensures that the main function is executed only when the script is run directly and not when it is imported as a module into another script.
• When the script is run directly, the main function is called, and the checksum calculation and printing take place.

Sample Output:

If we run this code with the provided sample data, the output will be:

Data: [1, 35, 69, 103, 137]

Calculated Checksum: 0x7e

In this example, the XOR operation is used as a simple checksum calculation technique. However, for more robust error detection, other checksum algorithms like CRC or Adler-32 are often preferred. The choice of the checksum algorithm depends on the specific requirements and the level of error detection needed for the application.

Error detection code - checksum offers several advantages in the realm of error detection for digital communication.

• Simple Implementation: Checksum is easy to implement, requiring minimal hardware and software resources. Its straightforward algorithm makes it accessible to various systems without adding significant complexity.
• Efficient Error Detection: Checksum codes are effective at detecting common errors that may occur during digital communication. By quickly identifying these errors, data integrity can be maintained without the need for elaborate error correction methods.
• Fast Computation: The checksum calculation process is swift due to its simplicity. It involves minimal operations, making it well-suited for real-time data packet transmissions where speed is crucial for seamless communication.
• Low Latency: With its minimal resource usage and limited computations, checksum introduces minimal delays in data processing. The low latency is advantageous for time-sensitive applications, ensuring efficient data validation and error detection without causing significant transmission delays. This attribute is particularly valuable in situations where real-time data transfer and response are essential.

Even though checksum has many advantages in error detection, it also has some drawbacks:

• Restricted Error Detection Capability: While checksum is easy to implement and can detect common errors, it is restricted to identifying only those errors that are commonly encountered in digital communication. Uncommon or complex errors may go undetected.
• Restricted Error Correction Capability: Checksum codes excel at error detection, but they lack the ability to correct the errors they find. As a result, it can only inform about the presence of errors without providing a mechanism to fix them.
• Vulnerability to Errors: The checksum algorithm can be vulnerable to errors, particularly when multiple errors occur within the same data packet. In such cases, the effectiveness of the checksum as an error detection tool may diminish.
• Data Packet Size Sensitivity: The efficiency of checksum is sensitive to the size of the data packet. If the packet size is too small, there is a higher chance of missing errors or failing to detect them. On the other hand, large data packets increase the overhead of checksum calculation, affecting overall performance. Careful consideration of the data packet size is necessary to achieve optimal results with checksum.

## Conclusion

Checksum plays a crucial role in ensuring data integrity and detecting errors in digital communication. It provides a simple and efficient means to detect corruption or tampering during transmission, safeguarding the accuracy and reliability of data exchanges.

As technology continues to advance, checksum algorithms will remain indispensable in various applications, from file transfers and network communications to data storage and cybersecurity.

If you want to master this crucial function, consider taking up a Computer Science certification from upGrad. With the course by upGrad you will be able to master checksum online. The courses by upGrad will also help you seek employment in leading roles in the tech industry.

## FAQs

1. What are the limitations of checksum?

Checksum has its constraints that include poor error detection and correction capability, susceptibility to errors, and sensitivity to the size of the data packet.

2. How many types of checksums are there?

There are 14 types of checksums.

3. Which checksum is better than MD5?

The hash value produced by the SHA-256 algorithm is 256 bits or 64 hexadecimal digits. According to recent research, it is significantly better than either MD5.

Director of Engineering

Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More

###### Get Free Career Counselling
+91
*
By clicking, I accept theT&Cand
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.

Talk to our experts. We’re available 24/7.

Indian Nationals

1800 210 2020

Foreign Nationals

+918045604032

## Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...