2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2012 The OpenLDAP Foundation.
5 * Portions Copyright 2000-2003 Kurt D. Zeilenga.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 /* This implements the Fowler / Noll / Vo (FNV-1) hash algorithm.
18 * A summary of the algorithm can be found at:
19 * http://www.isthe.com/chongo/tech/comp/fnv/index.html
24 #include <lutil_hash.h>
26 /* offset and prime for 32-bit FNV-1 */
27 #define HASH_OFFSET 0x811c9dc5U
28 #define HASH_PRIME 16777619
35 lutil_HASHInit( lutil_HASH_CTX *ctx )
37 ctx->hash = HASH_OFFSET;
46 const unsigned char *buf,
49 const unsigned char *p, *e;
69 lutil_HASHFinal( unsigned char *digest, lutil_HASH_CTX *ctx )
71 ber_uint_t h = ctx->hash;
73 digest[0] = h & 0xffU;
74 digest[1] = (h>>8) & 0xffU;
75 digest[2] = (h>>16) & 0xffU;
76 digest[3] = (h>>24) & 0xffU;
81 /* 64 bit Fowler/Noll/Vo-O FNV-1a hash code */
83 #define HASH64_OFFSET 0xcbf29ce484222325ULL
89 lutil_HASH64Init( lutil_HASH_CTX *ctx )
91 ctx->hash64 = HASH64_OFFSET;
100 const unsigned char *buf,
103 const unsigned char *p, *e;
104 unsigned long long h;
112 /* xor the bottom with the current octet */
115 /* multiply by the 64 bit FNV magic prime mod 2^64 */
116 h += (h << 1) + (h << 4) + (h << 5) +
117 (h << 7) + (h << 8) + (h << 40);
128 lutil_HASH64Final( unsigned char *digest, lutil_HASH_CTX *ctx )
130 unsigned long long h = ctx->hash;
132 digest[0] = h & 0xffU;
133 digest[1] = (h>>8) & 0xffU;
134 digest[2] = (h>>16) & 0xffU;
135 digest[3] = (h>>24) & 0xffU;
136 digest[4] = (h>>32) & 0xffU;
137 digest[5] = (h>>40) & 0xffU;
138 digest[6] = (h>>48) & 0xffU;
139 digest[7] = (h>>56) & 0xffU;
141 #endif /* HAVE_LONG_LONG */