[Vala] FNV Hash algorithm



Hi.

I'm     implementing    64bit    FNV-1    algorithm,    found    here:
http://isthe.com/chongo/src/fnv/hash_64.c , my code:

public class FNV : GLib.Object
        {
                private static const uint64 FNV_64_INIT = 0x84222325cbf29ce4ULL;
                private static const uint64 FNV_64_PRIME = 0x100000001b3ULL;
                
                public FNV () {}
                
                public static uint64 fnv_64 (string k) {
                        var len = k.length;
                        uint64 rv = FNV_64_INIT;
                        
                        for (int i = 0; i < len; i++) {
                                rv *= FNV_64_PRIME;
                                rv ^= k[i];
                        };
                        
                        return rv;
                }
        }

Without  utf8  specific  characters  (two-byte  ones)  i get the right
output.  With two-byte characters my output doesn't match the one from
C++ function used in Mysql:

ulonglong hash64(const void *buf, size_t len, ulonglong hval) {
   const unsigned char *bp = (const unsigned char*)buf;
   const unsigned char *be = bp + len;

   /* FNV-1 hash each octet of the buffer */
   for (; bp != be; ++bp) {
      /* multiply by the 64 bit FNV magic prime mod 2^64 */
      hval *= FNV_64_PRIME;
      /* xor the bottom with the current octet */
      hval ^= (ulonglong)*bp;
   }

   return hval;
}

hval is FNV_64_INIT at the start. Maybe someone have any thought about
what  i  am  doing wrong, i don't know C++, but i need to get the same
output as c++ function.

Thanks in advance.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]