robertklep / node-metrohash

Node.js bindings for MetroHash
MIT License
26 stars 8 forks source link

non-deterministic hash results #3

Closed kurttheviking closed 7 years ago

kurttheviking commented 7 years ago

I was evaluating this library and one thing caught my eye, not sure if it's intentional or not. In short, given the same input, it appears to be relatively easy to generate inconsistent hashes. For example:

const MetroHash128 = require('metrohash').MetroHash128;

const now = Date.now();
const seen = new Set();

for (let i = 0; i < 1000000; i += 1) {
  const hash = new MetroHash128(now);

  hash.update('Hello/1.0 (World;)');

  seen.add(hash.digest());
}

console.log(seen.keys());

Produced the following output:

SetIterator {
  '7367f8e233ea5037d82d94faec311316',
  'f71ba7b069656149839479c5d1823129'
}

If this is expected, I think it might be useful to note in the docs that the hash result is not necessarily the same each run. Thanks!

kurttheviking commented 7 years ago

(Note, it doesn't appear to matter what the seed value is originally set to.)

robertklep commented 7 years ago

Ouch, that's not expected!

I usually develop my code on macOS, and for that OS, there's only one unique key generated per run (as it should). However, I just installed the same version on a Linux server, and I'm getting the same inconsistent results that you are getting.

I'll take a look at it ASAP.

EDIT: the issue occurs when hashing strings, hashing buffers works okay.

robertklep commented 7 years ago

I just pushed v2.2.0 to the NPM repo that should fix this issue.

The interesting thing is that this issue also wasn't caught by Travis, which I'm pretty sure isn't running macOS.

kurttheviking commented 7 years ago

@robertklep thanks for the quick turnaround!