Support UHF hashing for >= UHASH_KEY_SIZE bytes.

This commit is contained in:
Matthias Vallentin 2013-08-01 19:15:28 +02:00
parent 2a0790c231
commit 34965b4e77
2 changed files with 22 additions and 6 deletions

View file

@ -1,6 +1,7 @@
// See the file "COPYING" in the main distribution directory for copyright. // See the file "COPYING" in the main distribution directory for copyright.
#include <typeinfo> #include <typeinfo>
#include <openssl/md5.h>
#include "Hasher.h" #include "Hasher.h"
#include "NetVar.h" #include "NetVar.h"
@ -82,15 +83,29 @@ Hasher::Hasher(size_t arg_k, size_t arg_seed)
seed = arg_seed; seed = arg_seed;
} }
UHF::UHF(size_t seed) UHF::UHF(size_t arg_seed)
: h(seed) : h(arg_seed)
{ {
seed = arg_seed;
} }
// This function is almost equivalent to HashKey::HashBytes except that it does
// not depend on global state and that we mix in the seed multiple times.
Hasher::digest UHF::hash(const void* x, size_t n) const Hasher::digest UHF::hash(const void* x, size_t n) const
{ {
assert(n <= UHASH_KEY_SIZE); if ( n <= UHASH_KEY_SIZE )
return n == 0 ? 0 : h(x, n); return n == 0 ? 0 : h(x, n);
unsigned char d[16];
MD5(reinterpret_cast<const unsigned char*>(x), n, d);
const unsigned char* s = reinterpret_cast<const unsigned char*>(&seed);
for ( size_t i = 0; i < 16; ++i )
d[i] ^= s[i % sizeof(seed)];
MD5(d, 16, d);
return d[0];
} }
DefaultHasher::DefaultHasher(size_t k, size_t seed) DefaultHasher::DefaultHasher(size_t k, size_t seed)

View file

@ -123,9 +123,9 @@ public:
* Constructs an H3 hash function seeded with a given seed and an * Constructs an H3 hash function seeded with a given seed and an
* optional extra seed to replace the initial Bro seed. * optional extra seed to replace the initial Bro seed.
* *
* @param seed The seed to use for this instance. * @param arg_seed The seed to use for this instance.
*/ */
UHF(size_t seed = 0); UHF(size_t arg_seed = 0);
template <typename T> template <typename T>
Hasher::digest operator()(const T& x) const Hasher::digest operator()(const T& x) const
@ -171,6 +171,7 @@ private:
static size_t compute_seed(size_t seed); static size_t compute_seed(size_t seed);
H3<Hasher::digest, UHASH_KEY_SIZE> h; H3<Hasher::digest, UHASH_KEY_SIZE> h;
size_t seed;
}; };