-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash.c
107 lines (83 loc) · 1.52 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <string.h>
#include <ctype.h>
unsigned int hashFnv(char *input)
{
unsigned int hash = 0x811C9DC5;
unsigned int i;
for (i = 0; i < strlen(input); i++)
{
unsigned int c = input[i];
if (c == ':')
{
// for us, we reset the hash function at a colon
hash = 0x811C9DC5;
continue;
}
if ((c - 0x41) <= 0x19)
{
c += 0x20;
}
hash *= 0x1000193;
hash ^= c;
}
return hash;
}
unsigned int hashVol(char *input)
{
unsigned int hash = 0;
unsigned int i;
for (i = 0; i < strlen(input); i++)
{
unsigned int c = input[i];
if (c == ':')
{
hash = 0;
continue;
}
if ((c - 0x41) <= 0x19)
{
c += 0x20;
}
// rotate left by 6
hash = (hash << 6) | (hash >> (32 - 6));
hash = c ^ hash;
}
return hash;
}
#define MOD_ADLER 65521
unsigned int hashAdl(char *input)
{
unsigned int a = 1, b = 0;
unsigned int i;
for (i = 0; i < strlen(input); ++i)
{
unsigned int c = input[i];
if (c == ':')
{
a = 1;
b = 0;
continue;
}
if ((c - 0x41) <= 0x19)
{
c += 0x20;
}
a = (a + c) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}
return (b << 16) | a;
}
int main(int argc, char *argv[])
{
unsigned int fnv, vol, adl;
if (argc != 2)
{
return 1;
}
fnv = hashFnv(argv[1]);
vol = hashVol(argv[1]);
adl = hashAdl(argv[1]);
printf("%s:\nfnv: %u (0x%08X)\nvol: %u (0x%08X)\nadl: %u (0x%08X)\n", argv[1], fnv, fnv, vol, vol, adl, adl);
return 0;
}