mirror of https://github.com/TheAlgorithms/C
Implemented CRC-32 Checksum-Algorithm
This commit is contained in:
parent
608c17b0fd
commit
c924079d60
26
hash/hash.c
26
hash/hash.c
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue