Checksum Code In C
The Checksum is an error-detection method that adds extra bits to the message after it has been broadcast in order to find any errors in the message. Therefore, this article fully covers all the information about the checksum error detection method.
What does Checksum Error Detection represent?
- For error detection, checksum is applied.
- The redundant bits tacked on to the real data are called a checksum.
- A hash sum or hash value may also be used to refer to a checksum.
- This technique generates a Checksum from the sender's side using a checksum generator.
- A checksum checker is used to verify whether the right data is received at the other end by comparing it to the checksum.
- Any length of communication may be subjected to the error detection procedure known as checksum.
- To guarantee that the entire range of data is effectively sent, checksum gives information about the transmission to the recipient.
Different types of Checksum Algorithms:
The checksum values are created by programmers using a variety of cryptographic hash methods.
Below are a few of them along with some brief introduction:
1. SHA-0:
The first hash algorithm of this kind was SHA-0, although it was briefly discarded in 1993.
2. SHA-1:
As of 2010, the hash function SHA-1 is not regarded as a secure hash function.
3. SHA-2 (224, 256, 384, 512):
This includes a few modifications from SHA-1, the previous version. There are six hash functions in the SHA-2 family, each with a hash value of 224, 256, 384, or 512 bits.
4. MD5:
It is not necessary for each file to have a different number because MD5 is a hash function that creates a checksum result. Therefore, if a hacker replaces a file's checksum value with another, MD5 is vulnerable.
Detecting the Errors by using Checksum:
The checksum is created at the sender end and verified at the receiver end.
See the picture below to see how Checksum detects errors.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c1.png)
Checksum at the Sender side:
Data is divided into equal-sized blocks by the sender, who then adds each block's data using the 1's complement method to obtain the total.
The checksum is then obtained by adding to the sum, and it is sent with the data frames.
The processes for generating the checksum on the sender's end are listed below:
Step 1: Divide the given data into "k" blocks of an equal number, or "N" bits in each of the "K" blocks of the message.
Step 2: Add up each of the "k" divisions.
Step 3: Add the carry bit if it exists.
Step 4: Next, determine the sum's complement, which is 1. Any binary integer can be converted to its 1's complement by simply replacing every 0 with 1 and every 1 with 0 (for example, 10001 becomes 01110). Checksum is the name given to this supplemented sum.
Step 5: A checksum is added to the message that will be delivered to the recipient.
Data with a checksum appended is now ready for sending.
Checksum at the Receiver side:
The receiver sends the checksum validator the data together with the checksum when it receives the data. At the receiver end, the Checksum is verified using the procedures listed below.
Step 1: Add each "K" data block individually.
Step 2: Add the carry bit in step 2 if it exists.
Step 3: Finding the sum's 1s complement is step three.
Step 4: Accept the data if the result we received is all zeros; else reject the data.
Example for Checksum Detection:
Assume the receiver is to receive the data 10011001111000100010010000100.
Sender Side:
Step 1: Divide the message into four chunks of equal size.
The message is divided into k sections in the figure below.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c2.png)
Step 2: Add all k blocks together.
The figure below shows how to add data for k parts.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c3.png)
Step 3: Add any carry that is generated to the sum.
A picture to add carry to the total is shown below.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c4.png)
Step 4: Calculate the sum's 1's complement, often known as a checksum.
An illustration of the result's 1's complement may be seen below.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c5.png)
Step 5: Include the message's checksum before sending it to the receiver.
Receiver Side:
Step 1: Add each and every block of the k's size.
An illustration of additional blocks on the receiver side can be seen below.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c6.png)
Step 2: Add the result of step 1 to any carry that was generated during the addition process.
The image below illustrates how carrying is added to the total.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c7.png)
Step 3: Determine a sum's complement by one. Accept the message if all the bits in the outcome are zero; else, reject the data.
An illustration of the sum's 1's complement may be seen below.
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c8.png)
In our example, we obtain the complement of the total in the form of all 0s, hence the data is accepted.
C program for implementation of the Checksum:
#include<stdio.h>
#include<math.h>
int sender_side(int b[10],int k)
{
int check_sum,sum=0,i;
printf("\n****SENDER SIDE****\n");
for(i=0;i<k;i++)
{
sum+=b[i];
}
printf("Sum is: %d",sum);
check_sum=~sum;
printf("\nSender side checksum is:%d",check_sum);
return check_sum;
}
int receiver_side(int c[10],int k,int s_check)
{
int check_sum,sum=0,i;
printf("\n\n****RECEIVER SIDE****\n");
for(i=0;i<k;i++)
sum+=c[i];
printf("Receiver side sum is:%d",sum);
sum=sum+s_check;
check_sum=~sum;
printf("\nReceiver side checksum is:%d",check_sum);
return check_sum;
}
int main()
{
int a[10],i,m,s_check,r_check;
printf("\nEnter size of string:");
scanf("%d",&m);
printf("\nEnter elements of Array:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
s_check=sender_side(a,m);
r_check=receiver_side(a,m,s_check);
if(r_check==0)
printf("\n\nNo Error Detected during Transmission\n\n");
else
printf("\n\nError Detected");
}
OUTPUT:
![Checksum Code In C](https://static.tutorialandexample.com/c/checksum-code-in-c8.png)