Last active
February 9, 2018 00:27
-
-
Save bryc/81f72abea26f3b950815ddcfb52b5014 to your computer and use it in GitHub Desktop.
A simple but usable checksum/hash algorithm, in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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