Skip to content

Instantly share code, notes, and snippets.

@bryc
Last active February 9, 2018 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryc/81f72abea26f3b950815ddcfb52b5014 to your computer and use it in GitHub Desktop.
Save bryc/81f72abea26f3b950815ddcfb52b5014 to your computer and use it in GitHub Desktop.
A simple but usable checksum/hash algorithm, in C.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
unsigned char *data;
int len = 0;
int cyrb32() {
uint32_t i, tmp, sum = 0xbf3931a8;
for(i = 32; i < len; i++) {
sum += (sum << 3) + data[i];
sum ^= sum >> 1;
}
for(tmp = sum; tmp > 0; tmp >>= 3) {
sum += (sum << 4) ^ tmp;
}
return sum;
}
int main() {
FILE *fp = fopen("lol.note", "rb");
fseek(fp, 0, SEEK_END);
len = ftell(fp);
rewind(fp);
data = malloc(len * 8);
fread(data, len, 1, fp);
fclose(fp);
printf("Filesize is: %d bytes\n", len);
printf("Checksum: %08X\n", cyrb32());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment