xref: /lighttpd1.4/src/algo_md.h (revision f2e4e83a)
1 /* algo_md.h - message digest (MD) wrapper (non-cryptographic)
2  *
3  * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
4  * License: BSD 3-clause (same as lighttpd)
5  */
6 #ifndef INCLUDED_ALGO_MD_H
7 #define INCLUDED_ALGO_MD_H
8 #include "first.h"
9 
10 
11 /*
12  * not cryptographically secure (and never intended to be)
13  */
14 
15 
16 /* DJB hash function for strings (djb2a) */
17 #define DJBHASH_INIT 5381
18 __attribute_pure__
19 static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash);
20 static inline uint32_t djbhash(const char *str, const uint32_t len, uint32_t hash)
21 {
22     const unsigned char * const s = (const unsigned char *)str;
23     for (uint32_t i = 0; i < len; ++i) hash = ((hash << 5) + hash) ^ s[i];
24     return hash;
25 }
26 
27 
28 /* Donald E. Knuth
29  * The Art Of Computer Programming Volume 3
30  * Chapter 6.4, Topic: Sorting and Search */
31 __attribute_pure__
32 static inline uint32_t dekhash (const char *str, const uint32_t len);
33 static inline uint32_t dekhash (const char *str, const uint32_t len)
34 {
35     const unsigned char * const s = (const unsigned char *)str;
36     uint32_t h = len;
37     for (uint32_t i = 0; i < len; ++i) h = (h << 5) ^ (h >> 27) ^ s[i];
38     return h;
39 }
40 
41 
42 #endif /* INCLUDED_ALGO_MD_H */
43