summaryrefslogtreecommitdiff
path: root/libmisc/hash.c
blob: 3814cec0c1129c2ca168b7624e1ecf61cdc98423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* libmisc/hash.c - General-purpose hash utilities
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <libmisc/hash.h>

/* djb2 hash */
void hash_init(hash_t *hash) {
	*hash = 5381;
}
void hash_write(hash_t *hash, void *dat, size_t len) {
	for (size_t i = 0; i < len; i++)
		*hash = (*hash * 33) + (hash_t)(((unsigned char *)dat)[i]);
}

/* utilities */
hash_t hash(void *dat, size_t len) {
	hash_t h;
	hash_init(&h);
	hash_write(&h, dat, len);
	return h;
}