-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash.c
78 lines (69 loc) · 1.5 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
#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;
}
}
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;
}