I am trying to implement an XOR Checksum algorithm, which takes in a binary file and performs the algorithm returning the checksum. This is what I have so far:
int checksum(char filename[])
{
ifstream infile(filename, ios::in);
if (infile.fail())
{
cout << "Unable to open input file: " << filename << endl;
return 0;
}
int checksum = 0;
while(infile.good())
{
int x;
infile.read((char*)&x, sizeof(int));
if(infile.good())
{
checksum = checksum ^ x;
}
}
infile.close();
return checksum;
}
Do you see anything wrong with this algorithm? Is there a possibility that 2 slightly different files may return the same exact checksum value? This occurred for me. For example:
File #1
...
0x7b
...
0x2b
...
File #2
...
0x77
...
0x27
...
This happened to me. The 2 files only had 2 differences at these locations.
The only thing I see is that you assume the file will be of a size perfectly divisible by sizeof(int). If that's not the case then 'x' will be undefined giving you unreliable results. You should zero x:
int x = 0;
Is there a possibility that 2 slightly different files may return the same exact checksum value?