Scala MurmurHash3 library not matching Python mmh3 library
<p>I have a need to MurmurHash strings in both Python and Scala. However they are giving very different results. Scala's builtin <code>MurmurHash3</code> library does not seem to give the same results as any of the other libraries I have tried including online ones. The odd thing is it seems to match on a single character but not multiple characters. Here are some examples:</p>

<p>Python:</p>

<pre><code>mmh3.hash('string', 0)
res: -1390314837
</code></pre>

<p>Scala:</p>

<pre><code>MurmurHash3.stringHash("string", 0)
res: 379569354
</code></pre>

<p>I have tried playing with signed and unsigned ints as I know Java has signed and the C implementation python is wrapping is using unsigned. But even using NumPy to convert to a signed int gives us no help. This website seems to agree with the python implementation: </p>

<p><a href="http://murmurhash.shorelabs.com/" rel="nofollow">http://murmurhash.shorelabs.com/</a></p>

<p>Any ideas on what could be going on here?</p>

<p>Scala uses Java strings which are encoded as UTF-16.  These are packed two at a time into an <code>Int</code>; Python uses a <code>char*</code> (8 bits), so packs in four characters at a time instead of two.</p>

<p>Edit: Scala also packs the chars in MSB order, i.e. <code>(s.charAt(i) &lt;&lt; 16) | (s.charAt(i+1))</code>.  You might need to switch to an array of shorts and then swap every pair of them if it's really important to get exactly the same answer.  (Or port the Scala code to Python or vice versa.)  It also finalizes with the string length; I'm not sure how Python incorporates length data, if it does at all.  (This is important so you can distinguish the strings <code>"\u0000"</code> and <code>"\u0000\u0000"</code>.)</p>

python scala encryption hash murmurhash