The following code snippet is about CRC8 hash function. Usually in embedded systems there is an already built-in function for CRC8-16-32 etc. However in case there is not, here is a simple approach for it.
CRCs are so called because the check (data verification) value is a redundancy (it expands the message without adding information) and the algorithm is based on cyclic codes. CRCs are popular because they are simple to implement in binary hardware, easy to analyze mathematically, and particularly good at detecting common errors caused by noise in transmission channels. Because the check value has a fixed length, the function that generates it is occasionally used as a hash function.
The CRC was invented by W. Wesley Peterson in 1961; the 32-bit CRC function of Ethernet and many other standards is the work of several researchers and was published in 1975.
char CRC8(const char *data,int length)
{
char crc = 0x00;
char extract;
char sum;
for(int i=0;i<length;i++)
{
extract = *data;
for (char tempI = 8; tempI; tempI--)
{
sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum)
crc ^= 0x8C;
extract >>= 1;
}
data++;
}
return crc;
}