Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- uint32_t crc_table[256];
- //hash table initialization, call this procedure beforehand we proceed to
- //the hashing function
- void initTable(void)
- {
- uint32_t i = 0;
- int j = 0;
- for (i = 0; i < 256; i++)
- {
- uint32_t c = i;
- for (j = 0; j < 8; j++)
- {
- c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
- }
- crc_table[i] = c;
- }
- }
- //this function generates the CRC checksum for the string we put in
- uint32_t makeCrc(uint8_t *buf, size_t len)
- {
- uint32_t c = 0xFFFFFFFF;
- size_t i = 0;
- for (i = 0; i < len; i++)
- {
- c = crc_table[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
- }
- return c ^ 0xFFFFFFFF;
- }
- int main()
- {
- //we initialize the string we want to hash
- const char *crcString = "The quick brown fox jumps over the lazy dog";
- //fill the hash table
- initTable();
- //print out
- printf("CRC hash: %X", makeCrc(crcString, strlen(crcString)));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment