I recently had some trouble finding a way to create an IP Header checksum function for a program I was developing in C#. The programs purpose was to send IP Packets with wrapped data through the network using winpcap. Of course I was sending the raw packets which means I was having to forge the Ethernet layer, TCP layer and IP layer packets. The problem is that the IP Header has a field that is the checksum result for the header. After a lot of searching through the net I came up with a function in C++ which I translated into C# with a few modifications. In my tests the function is working perfectly. Here follows the code:

public static ushort ComputeHeaderIpChecksum(byte[] header, int start, int length) {

    ushort word16;
    long sum = 0;
    for (int i = start; i < (length + start); i+=2) {
        word16 = (ushort)(((header[i] << 8 ) & 0xFF00)
        + (header[i + 1] & 0xFF));
        sum += (long)word16;

    while ((sum >> 16) != 0) {
        sum = (sum & 0xFFFF) + (sum >> 16);

    sum = ~sum;

    return (ushort)sum;



I hope this works for you. The first parameter is the byte array containing the IP Header packet (already formed but with the checksum field [two bytes] set to zero), then you have the start parameter which tells where the IP header starts in the array, and the last parameter is the length of the array. Hope it works for you, please leave your comments 😉