-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhash.c
164 lines (149 loc) · 3.43 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include "hash.h"
uint64_t AwareHash(unsigned char* data, uint64_t n,
uint64_t hash, uint64_t scale, uint64_t hardener) {
while (n) {
hash *= scale;
hash += *data++;
n--;
}
return hash ^ hardener;
}
uint64_t AwareHash_debug(unsigned char* data, uint64_t n,
uint64_t hash, uint64_t scale, uint64_t hardener) {
while (n) {
fprintf(stderr, " %lu %lu %lu %u\n", n, hash, scale, *data);
hash *= scale;
hash += *data++;
n--;
fprintf(stderr, " internal %lu\n", hash);
}
return hash ^ hardener;
}
/*
* mangle
*/
// void mangle(const unsigned char* key, unsigned char* ret_key,
// int nbytes) {
// unsigned long long new_key = 0;
// int i;
// for (i=0; i<nbytes; ++i) {
// new_key |= key[nbytes-i-1] << (i * 8);
// }
// new_key = (new_key * 2083697005) & (0xffffffff);
// for (i=0; i<nbytes; ++i) {
// ret_key[i] = (new_key >> (i * 8)) & 0xff;
// }
// }
//
/*
* mangle
*/
// void unmangle(const unsigned char* key, unsigned char* ret_key,
// int nbytes) {
// /*
// * 10001^-1 mod 2^32 = 3472992753
// * 1001^-1 mod 2^32 = 3054961753
// * 101^-1 mod 2^32 = 2083697005
// */
// unsigned long long new_key = 0;
// int i;
// for (i=0; i<nbytes; ++i) {
// new_key |= key[i] << (i * 8);
// }
// new_key = (new_key * 101) & (0xffffffff);
// for (i=0; i<nbytes; ++i) {
// ret_key[nbytes-i-1] = (new_key >> (i * 8)) & 0xff;
// }
// }
void mangle(const unsigned char* key, unsigned char* ret_key,
int nbytes) {
for (int i=0; i<nbytes; ++i) {
ret_key[i] = key[nbytes-i-1];
}
if (nbytes == 13) {
ret_key[0] = key[5];
ret_key[1] = key[11];
ret_key[2] = key[7];
ret_key[3] = key[6];
ret_key[4] = key[1];
ret_key[5] = key[9];
ret_key[6] = key[10];
ret_key[7] = key[4];
ret_key[8] = key[2];
ret_key[9] = key[8];
ret_key[10] = key[12];
ret_key[11] = key[0];
ret_key[12] = key[3];
}
}
void unmangle(const unsigned char* key, unsigned char* ret_key,
int nbytes) {
for (int i=0; i<nbytes; ++i) {
ret_key[i] = key[nbytes-i-1];
}
if (nbytes == 13) {
ret_key[0] = key[11];
ret_key[1] = key[4];
ret_key[2] = key[8];
ret_key[3] = key[12];
ret_key[4] = key[7];
ret_key[5] = key[0];
ret_key[6] = key[3];
ret_key[7] = key[2];
ret_key[8] = key[9];
ret_key[9] = key[5];
ret_key[10] = key[6];
ret_key[11] = key[1];
ret_key[12] = key[10];
}
}
// void unmangle(const unsigned char* key, unsigned char* ret_key,
// int nbytes) {
// /*
// * 10001^-1 mod 2^32 = 3472992753
// * 1001^-1 mod 2^32 = 3054961753
// * 101^-1 mod 2^32 = 2083697005
// */
// unsigned long long new_key = 0;
// int i;
// for (i=0; i<nbytes; ++i) {
// new_key |= key[i] << (i * 8);
// }
// new_key = (new_key * 101) & (0xffffffff);
// for (i=0; i<nbytes; ++i) {
// ret_key[nbytes-i-1] = (new_key >> (i * 8)) & 0xff;
// }
// }
uint64_t seed = 0;
uint64_t GenHashSeed(int index) {
/*
if (index == 0) {
srand(0);
}
*/
if (seed == 0) {
seed = rand();
}
uint64_t x, y = seed + index;
mangle((const unsigned char*)&y, (unsigned char*)&x, 8);
return AwareHash((uint8_t*)&y, 8, 388650253, 388650319, 1176845762);
}
int is_prime(int num) {
int i;
for (i=2; i<num; i++) {
if ((num % i) == 0) {
break;
}
}
if (i == num) {
return 1;
}
return 0;
}
int calc_next_prime(int num) {
while (!is_prime(num)) {
num++;
}
return num;
}