### How is TCP packet CheckSum Calculated? - Theory

Namaste Friends,

Today, I m going to share with you actual method to calculate the checksum of TCP header packet at the binary level.

#### Introduction

Are you working on a networking project where you created TCP/UDP packets from scratch but don't know how to find checksum of from pseudo TCP packets? well, friend, you come to the right place. here, I am going to share with you the actual binary concept to calculate the checksum of pseudo TCP packets. Before starting this tutorial, I want to highlight that for this tutorial you need basic information of binary number operations.

#### How it works?

I am assuming that you already aware of TCP pseudo concept. basically, to calculate the checksum of TCP packet we need to create a TCP pseudo packet to calculate the actual checksum of TCP packet. if you don't know about pseudo concept then, I think you need to take a look here.

Now, let's focus on theory again. first, we have to divide and slice binary data into 16 bits pieces. After slicing in 16-bit pieces, we have to do find out the binary addition of all sliced pieces. After getting the binary sum of 16-bit pieces, there is one more small trick before one's complement of the binary sum. actually, we just need to take a 4-bit number from starting point of binary number and then add it again with the main binary sum. At last, calculate one's complement of the binary sum. done! I hope now you got the basic concept of TCP Packet CheckSum.

#### Example To Understand Theory.

Step 1.

Let assume that We have 4 bytes of pseudo header data (32-bits).

```Binary Representation = 10000000111111100101011111001010
```

Step 2.

Divide binary Number Into 16-bit pieces.

```0                16-bit             32-bit
---------------------------------------
1000000011111110   0101011111001010
---------------------------------------
```

Step 3.

Calculate the sum of all sliced pieces of binary numbers.

```  0-bit           16-bit

1000000011111110

+ 0101011111001010

----------------------
01101100011001000
-----------------------
```

Step 4.

Add 4-bits of binary number into the calculated binary number. here, these 4 bits are nothing other than starting 4 digits of the same calculated binary number.

```

Calculated Binary Numbers =    01101100011001000

4-bit to add into same binary number = { 0110 } 1100011001000

0-bit           16-bit

01101100011001000

+              0110

----------------------
01101100011001110
-----------------------
```

Step 5.

Find One's Complement. (Just Change All 0's Into 1's).

```Binary Number    = 01101100011001110

One's Complement = 00010011100110001
```

Step 6.

Done! That One's Complemented binary number is our checksum. hence, with this method, you can easily find the correct checksum of any TCP/UDP packets.

`CheckSum = One's Complement = 00010011100110001`

I hope you enjoyed this tutorial.
if you have any suggestion or query.
comment below

have a nice day.