Implemented CRC-32 Checksum-Algorithm

This commit is contained in:
ChatN0ir 2019-10-13 12:13:00 +02:00
parent 608c17b0fd
commit c924079d60
3 changed files with 33 additions and 1 deletions

View File

@ -60,4 +60,28 @@ int adler_32(char s[])
i++;
}
return (b << 16) | a;
}
}
/* crc32 Hash-Algorithm*/
int crc32(char string[])
{
int i = 0;
unsigned int cur_crc, masking;
cur_crc = 0xFFFFFFFF;
while(string[i] != '\0')
{
unsigned int byte = string[i];
cur_crc = cur_crc ^ byte;
for(int j = 8; j > 0; --j)
{
masking = -(cur_crc & 1);
cur_crc = (cur_crc >> 1) ^ (0xEDB88320 & masking);
}
i++;
}
return -cur_crc;
}

View File

@ -40,4 +40,11 @@ char xor8(char[]);
*/
int adler_32(char[]);
/*
crc32: implements the crc-32 hash-algorithm
returns the checksum byte for the passed byte
*/
int crc32(char[]);
#endif

View File

@ -15,6 +15,7 @@ int main(void)
printf("djb2: %s --> %lld\n", s, djb2(s));
printf("xor8: %s --> %i\n", s, xor8(s)); /* 8 bit */
printf("adler_32: %s --> %i\n", s, adler_32(s)); /* 32 bit */
printf("crc32: %s --> %i\n", s, crc32(s));
return 0;
}