mscdex / node-xxhash

An xxhash binding for node.js
Other
193 stars 28 forks source link

different results from java #32

Open vast-z opened 5 years ago

vast-z commented 5 years ago

java code: long key = LongHashFunction.xx().hashChar('a'); String key = Long.toHexString(key); System.out.println(key);

result: e513e02c99167f96

js code: const xxhash = require('xxhash') const buffer = Buffer.from('a') const key = xxhash.hash64(buffer, 0).toString('hex') console.log(key)

result: 5b6e8ca9f1c44ed2

I don't know the reason, thacks for the author!!!

mscdex commented 5 years ago

I don't know, but it could be that the Java library you're using is based on a different version/configuration/implementation of xxhash that produces different results? This project uses xxhash's C implementation directly, so unless the Java library is also a binding directly to the C implementation, I would think this module would be more "correct."

vast-z commented 5 years ago

I agree with u, thank you very much. I found these lib (java/js) in xxhash web page, but different results bothered me.

vast-z commented 5 years ago

https://cyan4973.github.io/xxHash/ java: https://github.com/OpenHFT/Zero-Allocation-Hashing js(node): https://npmjs.org/package/xxhash

mscdex commented 5 years ago

Looking at the Java implementation I see the difference: they are converting the character to a two-byte value before hashing and they are returning the value reversed for some reason (although both implementations should be returning little endian always).

To reproduce the Java result, you'd have to do something like this:

const buffer = Buffer.from([0x61, 0x00]); 
const key = xxh.hash64(buffer, 0).swap64().toString('hex');
vast-z commented 5 years ago

thank you very much! You are so kind.I'll try that later…I was in a hurry yesterday…I made node call java finally…😄

felixhao28 commented 5 years ago

Same for python: https://pypi.org/project/xxhash/

wrobelma commented 4 years ago

Same for 32bit hash results: Java sample hash: f28bd32f (1111 0010 1000 1011 1101 0011 0010 1111)

JS sample hash decoded using big endian xxHash.hash(buffer, seed,'hex') 2fd38bf2 (0010 1111 1101 0011 1000 1011 1111 0010) JS sample hash decoded using little endian xxHash.hash(buffer, seed).toString(16) f28bd32f (1111 0010 1000 1011 1101 0011 0010 1111)

jakubriedl commented 4 years ago

Was switching JS libraries and found out that all other (js implementations) are returning reversed order. Don't know what is correct way. But had to do small inversion function to make it same output.

const xxhashOutput = require('xxhash').hash64(Buffer.from('text', 'utf8'), 0xcafebabe).toString('hex')
const xxhashjsOutput = require('xxhashjs').h64('text', 0xcafebabe).toString(16)
const inverted = '0'.repeat(16 - xxhashjsOutput.length) + xxhashjsOutput // it is missing leading 0s

let flipped = ''
while (flipped.length < inverted.length) {
  flipped = flipped + inverted.substr(inverted.length - flipped.length - 2, 2)
}

// flipped === xxhashOutput
console.log(flipped, xxhashOutput)