| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,37 @@ |
| 1 |
+#include "theft.h" |
|
| 2 |
+ |
|
| 3 |
+/* Fowler/Noll/Vo hash, 64-bit FNV-1a. |
|
| 4 |
+ * This hashing algorithm is in the public domain. |
|
| 5 |
+ * For more details, see: http://www.isthe.com/chongo/tech/comp/fnv/. */ |
|
| 6 |
+static const uint64_t fnv64_prime = 1099511628211L; |
|
| 7 |
+static const uint64_t fnv64_offset_basis = 14695981039346656037UL; |
|
| 8 |
+ |
|
| 9 |
+/* Init a hasher for incremental hashing. */ |
|
| 10 |
+void theft_hash_init(struct theft_hasher *h) {
|
|
| 11 |
+ h->accum = fnv64_offset_basis; |
|
| 12 |
+} |
|
| 13 |
+ |
|
| 14 |
+/* Sink more data into an incremental hash. */ |
|
| 15 |
+void theft_hash_sink(struct theft_hasher *h, uint8_t *data, size_t bytes) {
|
|
| 16 |
+ if (h == NULL || data == NULL) { return; }
|
|
| 17 |
+ uint64_t a = h->accum; |
|
| 18 |
+ for (size_t i = 0; i < bytes; i++) {
|
|
| 19 |
+ a = (a ^ data[i]) * fnv64_prime; |
|
| 20 |
+ } |
|
| 21 |
+ h->accum = a; |
|
| 22 |
+} |
|
| 23 |
+ |
|
| 24 |
+/* Finish hashing and get the result. */ |
|
| 25 |
+theft_hash theft_hash_done(struct theft_hasher *h) {
|
|
| 26 |
+ theft_hash res = h->accum; |
|
| 27 |
+ theft_hash_init(h); /* reset */ |
|
| 28 |
+ return res; |
|
| 29 |
+} |
|
| 30 |
+ |
|
| 31 |
+/* Hash a buffer in one pass. (Wraps the above functions.) */ |
|
| 32 |
+theft_hash theft_hash_onepass(uint8_t *data, size_t bytes) {
|
|
| 33 |
+ struct theft_hasher h; |
|
| 34 |
+ theft_hash_init(&h); |
|
| 35 |
+ theft_hash_sink(&h, data, bytes); |
|
| 36 |
+ return theft_hash_done(&h); |
|
| 37 |
+} |