taskrambler  0.1.8
Web server and task management solution.
utils/hash.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  * \author Unknown (find out)
6  *
7  * \copyright
8  * Copyright © 2012 Georg Hopp
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include <ctype.h>
25 #include <sys/types.h>
26 
27 #include <openssl/rand.h>
28 
29 #include "utils/hash.h"
30 
31 /**
32  * SDBM hashing algorithm:
33  *
34  * this algorithm was created for sdbm (a public-domain reimplementation of
35  * ndbm) database library. it was found to do well in scrambling bits,
36  * causing better distribution of the keys and fewer splits. it also happens
37  * to be a good general hashing function with good distribution. the actual
38  * function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
39  * is the faster version used in gawk. [there is even a faster, duff-device
40  * version] the magic constant 65599 was picked out of thin air while
41  * experimenting with different constants, and turns out to be a prime. this
42  * is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.
43  */
44 unsigned long
45 sdbm(const unsigned char * str, size_t len)
46 {
47  unsigned long hash = 0;
48 
49  for(; 0 < len; str++, len--)
50  hash = tolower(*str) + (hash << 6) + (hash << 16) - hash;
51 
52  return hash;
53 }
54 
55 // vim: set ts=4 sw=4:
unsigned long sdbm(const unsigned char *str, size_t len)
Definition: utils/hash.c:45