hashes.c
2.08 KB
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
/**
* This file contains some simple hash function
*
* Copyright (C) 2013 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @file
* @author Jason Linehan <patientulysses@gmail.com>
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
*/
#include "hashes.h"
uint32_t djb2_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 5381;
for (size_t i = 0; i < len; i++) {
hash = hash * 33 + buf[i];
}
return hash;
}
uint32_t sdbm_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 0;
for (size_t i = 0; i < len; i++) {
hash = buf[i] + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
uint32_t kr_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 0;
for (size_t i = 0; i < len; i++) {
hash += buf[i];
}
return hash;
}
uint32_t sax_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 0;
for (size_t i = 0; i < len; i++) {
hash ^= (hash << 5) + (hash >> 2) + buf[i];
}
return hash;
}
uint32_t dek_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 7919; /* prime */
for (size_t i = 0; i < len; i++) {
hash = (hash << 5) ^ (hash >> 27) ^ buf[i];
}
return hash;
}
uint32_t fnv_hash(const uint8_t *buf, size_t len)
{
uint32_t FNV_PRIME = 0x811C9DC5;
uint32_t hash = 0;
for (size_t i = 0; i < len; i++) {
hash *= FNV_PRIME;
hash ^= buf[i];
}
return hash;
}
uint32_t rotating_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 0;
for (size_t i = 0; i < len; i++) {
hash = (hash << 4) ^ (hash >> 28) ^ buf[i];
}
return hash;
}
uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len)
{
uint32_t hash = 786431; /* prime */
for (size_t i = 0; i < len; i++) {
hash += buf[i];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}