TheAlgorithms-C/hash/hash.c
2019-10-14 17:49:03 +02:00

80 lines
1.3 KiB
C

/*
author: Christian Bender
This is the implementation unit of the hash-functions.
Overview about hash-functions:
- sdbm
- djb2
- xor8 (8 bits)
- adler_32 (32 bits)
*/
long long sdbm(char s[])
{
long long hash = 0;
int i = 0;
while (s[i] != '\0')
{
hash = s[i] + (hash << 6) + (hash << 16) - hash;
i++;
}
return hash;
}
long long djb2(char s[])
{
long long hash = 5381; /* init value */
int i = 0;
while (s[i] != '\0')
{
hash = ((hash << 5) + hash) + s[i];
i++;
}
return hash;
}
char xor8(char s[])
{
int hash = 0;
int i = 0;
while (s[i] != '\0')
{
hash = (hash + s[i]) & 0xff;
i++;
}
return (((hash ^ 0xff) + 1) & 0xff);
}
int adler_32(char s[])
{
int a = 1;
int b = 0;
const int MODADLER = 65521;
int i = 0;
while (s[i] != '\0')
{
a = (a + s[i]) % MODADLER;
b = (b + a) % MODADLER;
i++;
}
return (b << 16) | a;
}
/* crc32 Hash-Algorithm*/
#include <inttypes.h>
uint32_t crc32(char* data){
int i = 0;
uint32_t crc = 0xffffffff;
while(data[i] != '\0'){
uint8_t byte = data[i];
crc = crc ^ byte;
for(int j = 8; j > 0; --j)
crc = (crc >> 1) ^ (0xEDB88320 & ( -(crc & 1)));
i++;
}
return crc ^ 0xffffffff;
}