mirror of
https://github.com/zeek/zeek.git
synced 2025-10-11 02:58:20 +00:00
Introduce global_hash_seed script variable.
This commit adds support for script-level specification of a seed to be used by hashers. For example, if the given name of a Bloom filter is not empty, then the seed used by the underlying hasher only depends on the Bloom filter name. If the name is empty, we check whether the user defined a non-empty global_hash_seed string variable at script and use it instead. If that script variable does not exist, then we fall back to the initial seed computed a Bro startup (which is affected ultimately by $BRO_SEED). See Hasher::MakeSeed for details.
This commit is contained in:
parent
af9e181731
commit
8ca76dd4ee
6 changed files with 82 additions and 67 deletions
|
@ -18,6 +18,20 @@ public:
|
|||
typedef hash_t digest;
|
||||
typedef std::vector<digest> digest_vector;
|
||||
|
||||
/**
|
||||
* Creates a valid hasher seed from an arbitrary string.
|
||||
*
|
||||
* @param data A pointer to contiguous data that should be crunched into a
|
||||
* seed. If 0, the function tries to find a global_hash_seed script variable
|
||||
* to derive a seed from. If this variable does not exist, the function uses
|
||||
* the initial seed generated at Bro startup.
|
||||
*
|
||||
* @param size The number of bytes of *data*.
|
||||
*
|
||||
* @return A seed suitable for hashers.
|
||||
*/
|
||||
static size_t MakeSeed(const void* data, size_t size);
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
|
@ -64,11 +78,9 @@ public:
|
|||
size_t K() const { return k; }
|
||||
|
||||
/**
|
||||
* Returns the hasher's name. If not empty, the hasher uses this descriptor
|
||||
* to seed its *k* hash functions. Otherwise the hasher mixes in the initial
|
||||
* seed derived from the environment variable `$BRO_SEED`.
|
||||
* Returns the seed used to construct the hasher.
|
||||
*/
|
||||
const std::string& Name() const { return name; }
|
||||
size_t Seed() const { return seed; }
|
||||
|
||||
bool Serialize(SerialInfo* info) const;
|
||||
static Hasher* Unserialize(UnserialInfo* info);
|
||||
|
@ -81,16 +93,15 @@ protected:
|
|||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param k the number of hash functions.
|
||||
* @param arg_k the number of hash functions.
|
||||
*
|
||||
* @param name A name for the hasher. Hashers with the same name
|
||||
* should provide consistent results.
|
||||
* @param arg_seed The seed for the hasher.
|
||||
*/
|
||||
Hasher(size_t k, const std::string& name);
|
||||
Hasher(size_t arg_k, size_t arg_seed);
|
||||
|
||||
private:
|
||||
size_t k;
|
||||
std::string name;
|
||||
size_t seed;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -104,12 +115,8 @@ public:
|
|||
* optional extra seed to replace the initial Bro seed.
|
||||
*
|
||||
* @param seed The seed to use for this instance.
|
||||
*
|
||||
* @param extra If not empty, this parameter replaces the initial
|
||||
* seed to compute the seed for t to compute the seed NUL-terminated
|
||||
* string as additional seed.
|
||||
*/
|
||||
UHF(size_t seed = 0, const std::string& extra = "");
|
||||
UHF(size_t seed = 0);
|
||||
|
||||
template <typename T>
|
||||
Hasher::digest operator()(const T& x) const
|
||||
|
@ -152,7 +159,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
static size_t compute_seed(size_t seed, const std::string& extra);
|
||||
static size_t compute_seed(size_t seed);
|
||||
|
||||
H3<Hasher::digest, UHASH_KEY_SIZE> h;
|
||||
};
|
||||
|
@ -169,9 +176,9 @@ public:
|
|||
*
|
||||
* @param k The number of hash functions to use.
|
||||
*
|
||||
* @param name The name of the hasher.
|
||||
* @param seed The seed for the hasher.
|
||||
*/
|
||||
DefaultHasher(size_t k, const std::string& name = "");
|
||||
DefaultHasher(size_t k, size_t seed);
|
||||
|
||||
// Overridden from Hasher.
|
||||
virtual digest_vector Hash(const void* x, size_t n) const /* final */;
|
||||
|
@ -197,9 +204,9 @@ public:
|
|||
*
|
||||
* @param k The number of hash functions to use.
|
||||
*
|
||||
* @param name The name of the hasher.
|
||||
* @param seed The seed for the hasher.
|
||||
*/
|
||||
DoubleHasher(size_t k, const std::string& name = "");
|
||||
DoubleHasher(size_t k, size_t seed);
|
||||
|
||||
// Overridden from Hasher.
|
||||
virtual digest_vector Hash(const void* x, size_t n) const /* final */;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue