Skip to content

Instantly share code, notes, and snippets.

@fakessh
Created November 14, 2012 18:29
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 fakessh/4073870 to your computer and use it in GitHub Desktop.
Save fakessh/4073870 to your computer and use it in GitHub Desktop.
compute hash from crc32 algorythmes
/**************************************************************
* *
* Fichier : crc32.c *
* Fonctions pour calculer le hash CRC32 *
* *
**************************************************************/
/* Table of CRCs of all 8-bit messages. */
unsigned long crc32_table[256];
/* Flag: has the table been computed? Initially false. */
int crc32_table_computed = 0;
/* Make the table for a fast CRC. */
void make_crc32_table(void)
{
unsigned long c;
int n, k;
for (n = 0; n < 256; n++) {
c = (unsigned long) n;
for (k = 0; k < 8; k++) {
if (c & 1) {
c = 0xedb88320L ^ (c >> 1);
} else {
c = c >> 1;
}
}
crc32_table[n] = c;
}
crc32_table_computed = 1;
}
unsigned long Update_CRC32(unsigned long crc, unsigned char *buf, int len)
{
unsigned long c = crc ^ 0xffffffffL;
int n;
if (!crc32_table_computed)
make_crc32_table();
for (n = 0; n < len; n++) {
c = crc32_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
}
return c ^ 0xffffffffL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment