forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
80 lines (70 loc) · 1.35 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
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;
}