From c924079d6066547e52669517f62c4270f367521c Mon Sep 17 00:00:00 2001 From: ChatN0ir Date: Sun, 13 Oct 2019 12:13:00 +0200 Subject: [PATCH] Implemented CRC-32 Checksum-Algorithm --- hash/hash.c | 26 +++++++++++++++++++++++++- hash/hash.h | 7 +++++++ hash/test_program.c | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hash/hash.c b/hash/hash.c index 220bb5ca..8f202d4b 100644 --- a/hash/hash.c +++ b/hash/hash.c @@ -60,4 +60,28 @@ int adler_32(char s[]) i++; } return (b << 16) | a; -} \ No newline at end of file +} + +/* 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; +} diff --git a/hash/hash.h b/hash/hash.h index 7497b1b2..1f0722ff 100644 --- a/hash/hash.h +++ b/hash/hash.h @@ -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 \ No newline at end of file diff --git a/hash/test_program.c b/hash/test_program.c index f9dcd299..54439e8e 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -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; } \ No newline at end of file