]> git.feebdaed.xyz Git - socialize.git/commitdiff
add fnv hasher
authorseantywork <seantywork@gmail.com>
Wed, 30 Jul 2025 06:52:18 +0000 (15:52 +0900)
committerseantywork <seantywork@gmail.com>
Wed, 30 Jul 2025 06:52:26 +0000 (15:52 +0900)
src/ctl.c

index 712cc16bbb03c53440ff4a43d0893d112ff4fcb0..b2334a6abe20d607f028856736b763e285185c30 100644 (file)
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -216,13 +216,48 @@ int idpw_verify(char* idpw, char *newid, uint8_t* newtoken){
 
 }
 
-
+static inline uint32_t _hashfunc(uint8_t* data, size_t size){
+
+       size_t nblocks = size / 8;
+       uint64_t hash = 2166136261u;
+       for (size_t i = 0; i < nblocks; ++i){
+               hash ^= (uint64_t)data[0] << 0 | (uint64_t)data[1] << 8 |
+                        (uint64_t)data[2] << 16 | (uint64_t)data[3] << 24 |
+                        (uint64_t)data[4] << 32 | (uint64_t)data[5] << 40 |
+                        (uint64_t)data[6] << 48 | (uint64_t)data[7] << 56;
+               hash *= 0xbf58476d1ce4e5b9;
+               data += 8;
+       }
+
+       uint64_t last = size & 0xff;
+       switch (size % 8){
+       case 7:
+               last |= (uint64_t)data[6] << 56; /* fallthrough */
+       case 6:
+               last |= (uint64_t)data[5] << 48; /* fallthrough */
+       case 5:
+               last |= (uint64_t)data[4] << 40; /* fallthrough */
+       case 4:
+               last |= (uint64_t)data[3] << 32; /* fallthrough */
+       case 3:
+               last |= (uint64_t)data[2] << 24; /* fallthrough */
+       case 2:
+               last |= (uint64_t)data[1] << 16; /* fallthrough */
+       case 1:
+               last |= (uint64_t)data[0] << 8;
+               hash ^= last;
+               hash *= 0xd6e8feb86659fd93;
+       }
+
+       //return (uint32_t)(hash ^ hash >> 32);
+       return hash;
+}
 
 int make_hash(int fd, int buck_size){
 
-    int hash = fd % buck_size;
+    int hash = _hashfunc((uint8_t*)&fd, sizeof(int));
 
-    return hash;
+    return hash % buck_size;
 }