In an attempt to tamper proof my executable with some hashing I've come across some unexpected and unknown compiler behaviour.
I have this idea of calculating a simple hash value of my program by simply adding all the bytes in the binary and using the remainder of that total divided by 256 as a hash value.
I figured that if in my program I have a string literal of "0000" and that the hash I calculate with the above algorithm gives for example, 163, that I could get 164 by replacing one of the '0' with a '1'. However the results I get are far from that. By replacing a single character by a single value I get a totally different hash result. For this example the 163 could become 84, which puzzles me.
My function for calculating the hash is quite simple but here it is anyway.
int calcHash(const char *filename) {
int hash = 0;
int c;
FILE *fh = fopen(filename, "r");
if (!fh)
return -1;
while((c = fgetc(fh)) != EOF)
hash = (hash + c) % 256;
fclose(fh);
return hash;
}
And I call it from main() with this statement.
int hash = calcHash(argv[0]);
What exactly happens during compilation? Anyway I can get more predictable hash values?
Perhaps this doesn't happen with all compilers. I am just totally baffled with that unexpected behaviour.
I'm using gcc on Ubuntu.