]> git.sur5r.net Git - openldap/blob - libraries/liblutil/hash.c
cleanup
[openldap] / libraries / liblutil / hash.c
1 /* $OpenLDAP$ */
2 /* This implements the Fowler / Noll / Vo (FNV-1) hash algorithm.
3  * A summary of the algorithm can be found at:
4  *   http://www.isthe.com/chongo/tech/comp/fnv/index.html
5  */
6
7 #include "portable.h"
8
9 #include <lutil_hash.h>
10
11 /* offset and prime for 32-bit FNV-1 */
12 #define HASH_OFFSET     0x811c9dc5U
13 #define HASH_PRIME      16777619
14
15
16 /*
17  * Initialize context
18  */
19 void
20 lutil_HASHInit( struct lutil_HASHContext *ctx )
21 {
22         ctx->hash = HASH_OFFSET;
23 }
24
25 /*
26  * Update hash
27  */
28 void
29 lutil_HASHUpdate(
30     struct lutil_HASHContext    *ctx,
31     const unsigned char         *buf,
32     ber_len_t           len
33 )
34 {
35         const unsigned char *p, *e;
36         ber_uint_t h;
37
38         p = buf;
39         e = &buf[len];
40
41         h = ctx->hash;
42
43         while( p < e ) {
44                 h *= HASH_PRIME;
45                 h ^= *p++;
46         }
47
48         ctx->hash = h;
49 }
50
51 /*
52  * Save hash
53  */
54 void
55 lutil_HASHFinal( unsigned char *digest, struct lutil_HASHContext *ctx )
56 {
57         ber_uint_t h = ctx->hash;
58
59         digest[0] = h & 0xffU;
60         digest[1] = (h>>8) & 0xffU;
61         digest[2] = (h>>16) & 0xffU;
62         digest[3] = (h>>24) & 0xffU;
63 }