Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C file checksum

Tags:

c

checksum

how can i make a checksum of a file using C? i dont want to use any third party, just default c language and also speed is very important (its less the 50mb files but anyway)

thanks

like image 485
Checksummmmm Avatar asked Aug 12 '10 00:08

Checksummmmm


People also ask

What is checksum in C?

The Checksum is an error detection method that detected errors in data/message while it is transmitted from sender to receiver. This method is used by the higher layer protocols and makes use of the Checksum Generator on the Sender side and Checksum Checker on the Receiver side.

How does sha256 checksum work?

An SHA-256 checksum is a sequence of numbers and letters that you can use to check that your copy of a downloaded update file is identical to the original. Small changes in a file produce very different looking checksums. A single character difference produces a very different looking checksum.

What is the checksum of a file?

A checksum is a string of numbers and letters used to uniquely identify a file. Checksum is most commonly used to verify if a copy of a file is identical to an original, such as downloaded copies of ArcGIS product installation or patch files.


2 Answers

I would suggest starting with the simple one and then only worrying about introducing the fast requirement if it turns out to be an issue.

Far too much time is wasted on solving problems that do not exist (see YAGNI).

By simple, I mean simply starting a checksum character (all characters here are unsigned) at zero, reading in every character and subtracting it from the checksum character until the end of the file is reached, assuming your implementation wraps intelligently.

Something like in the following program:

#include <stdio.h>

unsigned char checksum (unsigned char *ptr, size_t sz) {
    unsigned char chk = 0;
    while (sz-- != 0)
        chk -= *ptr++;
    return chk;
}

int main(int argc, char* argv[])
{
    unsigned char x[] = "Hello_";
    unsigned char y = checksum (x, 5);
    printf ("Checksum is 0x%02x\n", y);
    x[5] = y;
    y = checksum (x, 6);
    printf ("Checksum test is 0x%02x\n", y);
    return 0;
}

which outputs:

Checksum is 0x0c
Checksum test is 0x00

That checksum function actually does both jobs. If you pass it a block of data without a checksum on the end, it will give you the checksum. If you pass it a block with the checksum on the end, it will give you zero for a good checksum, or non-zero if the checksum is bad.

This is the simplest approach and will detect most random errors. It won't detect edge cases like two swapped characters so, if you need even more veracity, use something like Fletcher or Adler.

Both of those Wikipedia pages have sample C code you can either use as-is, or analyse and re-code to avoid IP issues if you're concerned.

like image 150
paxdiablo Avatar answered Sep 25 '22 21:09

paxdiablo


  1. Determine which algorithm you want to use (CRC32 is one example)
  2. Look up the algorithm on Wikipedia or other source
  3. Write code to implement that algorithm
  4. Post questions here if/when the code doesn't correctly implement the algorithm
  5. Profit?
like image 41
Paul Tomblin Avatar answered Sep 25 '22 21:09

Paul Tomblin