C program to implement CHECKSUM

In computing, a checksum is a small-sized data value computed 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 during transmission or storage.

Syntax

unsigned int checksum(char *data);
// Returns computed checksum value for given data

Why Use CHECKSUM?

There are several reasons why checksums are used

  • Error detection Detect errors that occur during data transmission or storage
  • Data integrity Ensure data hasn't been modified during transmission
  • Authentication Verify authenticity using secret keys
  • Space efficient Small signature represents large data
  • Time efficient Faster than comparing entire data sets
  • Easy to implement Simple algorithms with low overhead

How to Implement CHECKSUM

The general steps for implementing a checksum are

  1. Choose an appropriate checksum algorithm (CRC, MD5, SHA, etc.)
  2. Implement the algorithm in code
  3. Calculate checksum for original data
  4. Compare checksum of received data with original
  5. Take action if checksums don't match

Example: Simple String Checksum

Here's a C program that calculates a simple checksum by summing ASCII values of characters

#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!";
    unsigned int original_checksum = checksum(str);
    
    printf("Original string: '%s'
", str); printf("Checksum: %u
", original_checksum); // Simulate data corruption str[0] = 'h'; // Change 'H' to 'h' unsigned int modified_checksum = checksum(str); printf("Modified string: '%s'
", str); printf("New checksum: %u
", modified_checksum); if (original_checksum == modified_checksum) { printf("Data integrity maintained
"); } else { printf("Data corruption detected!
"); } return 0; }
Original string: 'Hello, World!'
Checksum: 1129
Modified string: 'hello, World!'
New checksum: 1161
Data corruption detected!

Example: 16-bit Checksum Algorithm

A more robust approach uses 16-bit checksum with overflow handling

#include <stdio.h>
#include <string.h>

unsigned short checksum16(char *data) {
    unsigned int sum = 0;
    int len = strlen(data);
    
    // Process data in 16-bit chunks
    for (int i = 0; i < len - 1; i += 2) {
        unsigned short word = (data[i] << 8) + data[i + 1];
        sum += word;
    }
    
    // Handle odd length
    if (len % 2 == 1) {
        sum += data[len - 1] << 8;
    }
    
    // Add carry bits
    while (sum >> 16) {
        sum = (sum & 0xFFFF) + (sum >> 16);
    }
    
    return (unsigned short)(~sum);
}

int main() {
    char data[] = "TutorialsPoint";
    unsigned short cs = checksum16(data);
    
    printf("Data: %s
", data); printf("16-bit checksum: 0x%04X (%u)
", cs, cs); return 0; }
Data: TutorialsPoint
16-bit checksum: 0x9C8F (40079)

Key Points

  • Simple checksums are fast but vulnerable to collisions
  • 16-bit checksums provide better error detection than simple sum
  • For security purposes, use cryptographic hash functions like SHA-256
  • Always compare checksums to detect data corruption

Conclusion

Checksums are essential for data integrity verification in C programs. While simple algorithms work for basic error detection, more sophisticated methods like 16-bit checksums or cryptographic hashes provide better reliability for critical applications.

Updated on: 2026-03-15T14:27:05+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements