Skip to content

Why Murmur3 hashes differ from guava hashes? #3

@jcornaz

Description

@jcornaz

Hello,

My understanding is that Murmur3.hash_x64_128 (of this project) should return the same result (in bytes) as Hasing.murmur3_128().hashBytes (of guava library).

But It doesn't. May I ask if it is expected to be different and why?

Here is my code, just in case I made an obvious mistake you could point out. (the code is written Kotlin, but should be easily understandable)

import com.google.common.hash.Hashing
import com.sangupta.murmur.Murmur3
import org.junit.Test
import java.nio.ByteBuffer
import java.util.*
import kotlin.test.assertTrue

private const val SEED = 42

class Murmur3Test {

  @Test
  fun murmur32shouldCorrespondToGuavaHashes() {
    val guava = Hashing.murmur3_32(SEED)
    repeat(1000) {
      val data = UUID.randomUUID().toByteArray()

      val guavaResult = guava.hashBytes(data).asBytes()
      val murmurResult = Murmur3.hash_x86_32(data, 16, SEED.toLong()).asBytes()

      assertTrue(Arrays.equals(guavaResult, murmurResult))
    }
  }

  @Test
  fun murmur128shouldCorrespondToGuavaHashes() {
    val guava = Hashing.murmur3_128(SEED)
    repeat(1000) {
      val data = UUID.randomUUID().toByteArray()

      val guavaResult = guava.hashBytes(data).asBytes()
      val murmurResult = Murmur3.hash_x64_128(data, 16, SEED.toLong()).asBytes()

      assertTrue(Arrays.equals(guavaResult, murmurResult))
    }
  }
}

fun UUID.toByteArray(): ByteArray {
  val buffer = ByteBuffer.allocate(16)

  buffer.putLong(mostSignificantBits)
  buffer.putLong(leastSignificantBits)

  return buffer.array()
}

fun LongArray.asBytes(): ByteArray {
  val buffer = ByteBuffer.allocate(size * 8)

  forEach { buffer.putLong(it) }

  return buffer.array()
}

fun Long.asBytes(): ByteArray {
  val buffer = ByteBuffer.allocate(8)

  buffer.putLong(this)

  return buffer.array()
}

Metadata

Metadata

Assignees

Labels

wontfixAs designed, Not worth the effort

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions