2

I want to use the MurmurHash2 algorithm (32bit) for download verification in a Java application. There exist various implementations on GitHub, but I want to use a MessageDigest implementation, as I am interested in how to "translate" a hashing algorithm in this way and it would offer the possibility to use a DigestInputStream to calculate hashes "on the fly" while downloading.

My Question: Is this straightforward/easily done? Is it impossible? Can you point me to further resources how to do this?

  • AFAIK you should create also your own SecurityProvider if you want use MessageDigest in java way, with `MessageDigest.getInstance` Looks like it not too difficult, if you take code, for example, from https://github.com/tnm/murmurhash-java/blob/master/src/main/java/ie/ucd/murmur/MurmurHash.java. – user1516873 Aug 02 '17 at 14:47
  • My problem is that the entire data to be hashed is known to the algorithm right from the beginning. If you look at MessageDigest's abstract methods, you see that the message digest has a mutable state, and the instance can continously be updated via the `update` methods, and finally asked to produce the hash via a `digest` method. TL;DR: I want to implement it in a "streaming" way. –  Aug 02 '17 at 15:47
  • if you look on implementation, in main loop, you can see data handled in blocks by 4 and changes internal state variable h. Last bytes handles differently, so you should maintain internal buffer for that. In `update` hash all expect %4 bytes, reminder store in internal buffer. In `digest` take care of this buffer an do final actions (^ and >>). – user1516873 Aug 03 '17 at 08:14
  • thanks, I will try my best and add an answer if I succeed –  Aug 03 '17 at 14:21
  • I managed to do what you said, with one exception: the initial value of `h`, which is not known in the beginning as `length` is involved. Setting `h` to `1` and multiplying `seed ^ length` in the end does not work, as `((..((seed ^ length) * m0) ^ k0) * m1) ^ k1)..))` does not equal `(seed ^ length) * ((1) * m0) ^ k0) * m1) ^ k1)..))`. Is this even possible? –  Aug 04 '17 at 01:16
  • 2
    a link to my current implementation: https://pastebin.com/vcyns9yY (the `void engineUpdate(byte[] input, int offset, int len)` still needs to be optimized) –  Aug 04 '17 at 01:29

0 Answers0