javascriptpythonmurmurhash

Murmurhash3 between python and javascript


So, I'm working on a project and I'm trying to get the same result in Python as in JavaScript, using x64.hash128.

>>> mmh3.hash128('Helo', 0)
36257070446276374087060059272450503274
>>> mmh3.hash128('Helo', 0, False)
20867082311197569335197338913536872358

While using the JavaScript method:

murmurHash3.x64.hash128("Helo")
'29c3815d749d726a1b46db969b492ae8'

Have you any idea how could I fix this? I tried translating myself but, at the end, it wasn't working and gave me a headache.

Library used:

  1. https://pypi.org/project/mmh3/
  2. https://github.com/pid/murmurHash3js

Solution

  • After some research, the JS outputs the hexadecimal value, and the problem is with the byte order differences. Python outputs a 128-bit integer as a single large integer, on the other hand, JavaScript splits the hash into two 64-bit segments.

    hash_int = mmh3.hash128('Helo', 0)
    
    # Splitting it into two 64-bit segments and reversing them
    high = (hash_int >> 64) & ((1 << 64) - 1)  
    low = hash_int & ((1 << 64) - 1)            
    
    # Converting each part to hex and reversing the order
    hash_hex = f'{low:016x}{high:016x}'
    
    print(hash_hex)  #expected output:'29c3815d749d726a1b46db969b492ae8'