# C program to implement CHECKSUM

## What is CHECKSUM?

In computing, a checksum is a small-sized data created from a larger data set using an algorithm, with the intention that any changes made to the larger data set will result in a different checksum. Checksums are commonly used to verify the integrity of data that has been transmitted or stored, as errors or modifications in the data can cause the checksum to change. They can also be used to verify the authenticity of the data, as the checksum is often generated using a secret key known only to the sender and receiver.

## Why we use CHECKSUM?

There are several reasons why checksums are used −

• Error detection − Checksums are used to detect errors that may occur during data transmission or storage. If the checksum of the received data does not match the original checksum, it indicates that errors have occurred and the data should be retransmitted.

• Data integrity − Checksums are used to ensure that the data has not been modified during transmission or storage. This is important for maintaining the integrity of the data and ensuring that it is the same as when it was originally sent.

• Authentication − Checksums can be used to verify the authenticity of data, as the checksum is often generated using a secret key known only to the sender and receiver. This helps prevent unauthorized parties from tampering with the data.

• Space Efficient − in cases when sending whole data over network is not efficient (too large), a checksum can be sent as a small signature of the data, which can be compared at the destination.

• Time Efficient − In case of large data it is time efficient to calculate and compare checksum rather than send and comparing the whole data.

• Easy to implement − checksum algorithms are generally simple to implement, which makes them a convenient option for a wide range of applications.

• Cost-effective − checksums do not require a lot of computational resources and do not add a significant amount of overhead to data transmission and storage, making them a cost-effective option for error detection and data integrity verification.

• Portable − checksums are a widely used technique and are supported by many different operating systems, networking protocols, and storage devices, which makes them portable across different platforms.

• Meets different requirements − There are different types of checksum algorithms such as CRC, MD5, SHA etc that are best suited to different use cases. This flexibility allows them to be used in a variety of applications with varying requirements.

• Secure − With the use of cryptographic hash functions, checksums can be made very secure, making them difficult to forge.

## How to implement CHECKSUM?

There are different ways to implement a checksum, depending on the specific requirements of the application and the type of data that needs to be checked. Here are the general steps for implementing a checksum −

• Choose an appropriate checksum algorithm. There are several different checksum algorithms available, such as CRC, MD5, and SHA. Each algorithm has its own strengths and weaknesses, and it's important to choose one that is appropriate for your specific use case.

• Implement the chosen algorithm in code. Depending on the programming language and platform you're using, there may be libraries available that provide implementations of the chosen algorithm. If not, you'll need to implement the algorithm yourself.

• Calculate the checksum for the original data. Use the implemented algorithm to calculate the checksum for the original data. This checksum value should be stored or transmitted along with the data.

• Compare the checksum of received data. When the data is received, calculate the checksum of the received data using the same algorithm. Compare this value to the original checksum that was transmitted or stored.

• Take appropriate action if the checksums do not match. If the calculated checksum of the received data does not match the original checksum, it indicates that errors or modifications have occurred and the data should be retransmitted or rejected.

• Depending on the application and requirements some more security features can be added like cryptographic hash function, salting, nonce, etc.

It is also worth noting that in some cases using checksums along with other methods like Error Correction Codes or Error detection and correction codes (ECC/EDC) can provide more robustness to errors, modification, and data authenticity.

## C program to implement CHECKSUM

Here's an example of a C program that calculates the checksum of a given string −

#include <stdio.h>
unsigned int checksum(char *str) {
unsigned int sum = 0;
while (*str) {
sum += *str;
str++;
}
return sum;
}
int main() {
char str[] = "Hello, World!";
printf("Checksum of '%s' is %u", str, checksum(str));
return 0;
}


This program uses a simple algorithm to calculate the checksum of a string. It initializes a variable sum to zero and iterates through each character in the string. For each character, it adds the value of the character to the sum variable. The final value of sum is returned as the checksum of the string.

Please be aware that the function above is just a simple example, and this type of checksum is not recommended for cryptographic or security purposes and is vulnerable to collisions.