aboutsummaryrefslogtreecommitdiff
path: root/hash/src/fnv1a.c
diff options
context:
space:
mode:
Diffstat (limited to 'hash/src/fnv1a.c')
-rw-r--r--hash/src/fnv1a.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/hash/src/fnv1a.c b/hash/src/fnv1a.c
new file mode 100644
index 0000000..32cb538
--- /dev/null
+++ b/hash/src/fnv1a.c
@@ -0,0 +1,23 @@
1#include <fnv1a.h>
2
3#include <assert.h>
4
5#define FNV_PRIME 2166136261
6#define FNV_OFFSET_BASIS 16777619
7
8uint32_t fnv1a_32(const void* buffer, size_t size) {
9 assert(buffer);
10 return fnv1a32_update(fnv1a32_begin(), buffer, size);
11}
12
13uint32_t fnv1a32_begin() { return FNV_PRIME; }
14
15uint32_t fnv1a32_update(uint32_t hash, const void* buffer, size_t size) {
16 assert(buffer);
17 const uint8_t* bytes = buffer;
18 for (size_t i = 0; i < size; i++) {
19 hash = hash ^ bytes[i];
20 hash = hash * FNV_OFFSET_BASIS;
21 }
22 return hash;
23}