-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.cpp
89 lines (63 loc) · 1.56 KB
/
hash.cpp
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
#include <stdint.h>
#include <string>
#include <openssl/md5.h>
#include "str.h"
std::string md5hex(const std::string & in)
{
unsigned char result[MD5_DIGEST_LENGTH];
MD5((unsigned char *)in.c_str(), in.size(), result);
std::string rc;
for(int i=0; i<MD5_DIGEST_LENGTH; i++)
rc += myformat("%02x", result[i]);
return rc;
}
void md5bin(const uint8_t *const in, const size_t len, uint8_t *const h_out)
{
MD5(in, len, h_out);
}
uint64_t MurmurHash64A(const void *const key, const int len, const uint64_t seed)
{
const uint64_t m = 0xc6a4a7935bd1e995LLU;
const int r = 47;
uint64_t h = seed ^ (len * m);
const uint64_t *data = (const uint64_t *)key;
const uint64_t *end = (len >> 3) + data;
while(data != end) {
uint64_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const uint8_t *data2 = (const uint8_t *)data;
switch(len & 7) {
case 7: h ^= (uint64_t)(data2[6]) << 48;
case 6: h ^= (uint64_t)(data2[5]) << 40;
case 5: h ^= (uint64_t)(data2[4]) << 32;
case 4: h ^= (uint64_t)(data2[3]) << 24;
case 3: h ^= (uint64_t)(data2[2]) << 16;
case 2: h ^= (uint64_t)(data2[1]) << 8;
case 1: h ^= (uint64_t)(data2[0]);
h *= m;
};
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
uint32_t crc32(const uint8_t *const data, const size_t n_data, const uint32_t polynomial)
{
const uint32_t p[] = { 0, polynomial };
uint32_t crc = 0xFFFFFFFF;
for(size_t i=0; i<n_data; i++) {
uint8_t ch = data[i];
for(size_t j=0; j<8; j++) {
bool b = (ch ^ crc) & 1;
crc >>= 1;
crc ^= p[b];
ch >>= 1;
}
}
return ~crc;
}