#include "lib.h"
#include "hash.h"
#include "primes.h"
+#include "randgen.h"
#include "xxh64.h"
#include <ctype.h>
HASH_TABLE_OP_RESIZE
};
+uint64_t hash_iv;
+
static bool hash_table_resize(struct hash_table *table, bool grow);
void hash_table_create(struct hash_table **table_r, pool_t node_pool,
hash_table_thaw(dest);
}
+void hash_init(void)
+{
+ random_fill(&hash_iv, sizeof(hash_iv));
+}
+
unsigned int str_hash(const char *p)
{
- return xxh64_to_32(xxh64_data(p, strlen(p), 0));
+ return xxh64_to_32(xxh64_data(p, strlen(p), hash_iv));
}
unsigned int str_stable_hash(const char *p)
struct xxh64_context ctx;
unsigned char c;
- xxh64_init(&ctx, 0);
+ xxh64_init(&ctx, hash_iv);
while (*p != '\0') {
c = (unsigned char)i_toupper(*p++);
xxh64_loop(&ctx, &c, 1);
/* Returns 0 if the pointers are equal. */
typedef int hash_cmp_callback_t(const void *p1, const void *p2);
+/* Random per-process IV to use for hash functions */
+extern uint64_t hash_iv;
+
/* Create a new hash table. If initial_size is 0, the default value is used.
table_pool is used to allocate/free large hash tables, node_pool is used
for smaller allocations and can also be alloconly pool. The pools must not
#define hash_table_copy(table1, table2) \
hash_table_copy((table1)._table, (table2)._table)
-/* hash function for strings */
+/* Hash function for strings. These are safe against collision attacks. They
+ are initialized with a per-process random key. */
unsigned int str_hash(const char *p) ATTR_PURE;
unsigned int strcase_hash(const char *p) ATTR_PURE;
+/* Like str_hash(), but there is no per-process random key. This isn't as safe
+ against collision attacks. */
unsigned int str_stable_hash(const char *p) ATTR_PURE;
/* fast hash function which uppercases a-z. Does not work well
/* a generic hash for a given memory block */
unsigned int mem_hash(const void *p, unsigned int size) ATTR_PURE;
+void hash_init(void);
+
#endif
#include "array.h"
#include "event-filter.h"
#include "env-util.h"
+#include "hash.h"
#include "hostpid.h"
#include "ipwd.h"
#include "process-title.h"
{
i_assert(!lib_initialized);
random_init();
+ hash_init();
data_stack_init();
hostpid_init();
lib_open_non_stdio_dev_null();