I am trying to replicate the file hashing of MobileSheetsPro, an Android app, where there is a hashcodes.txt which includes a hash for each file, as well as the path, last modified date and filesize. We'll just focus on the hashing part.
So for the random song I uploaded here if you want to try it for yourself, I am using the murmurhash-native
npm package to convert it to a buffer and then hash it like so:
const fs = require("fs");
const { promisify } = require("util");
const { murmurHash } = require("murmurhash-native");
const readFileAsync = promisify(fs.readFile);
async function hashcodeObjFromFilePath(filepath) {
const buf = await readFileAsync(filepath);
const h = murmurHash(buf);
console.log(h);
}
This prints out a hash of 4275668817
when using the default seed of 0 and 3020822739
when using the seed 0xc58f1a7b
as a second argument.
The problem: the app seems to calculate it differently. The developer wrote the following, but I don't see that exact function in the code he linked:
Check this out: github link
Those are the classes I've utilized. I call Hashing.goodFast32Hash(HASH_KEY)) where HASH_KEY is equal to 0xC58F1A7B.
EDIT I've got more infos from the dev:
I call Files.hash(file, Hashing.goodFast32Hash(HASH_KEY)); Using the return value from that, I call "asInt()" on the HashCode object that is returned. So it's a signed integer value (negative values are just fine). And yes, HASH_KEY is the seed value passed to the function.
Since I'm not good at Java I still have no idea to replicate this in node-js...
That's all the info I have, folks. Anyone see where I'm going wrong?
Found it! The asInt() function in the Java lib returns a signed int32 *in little endian byte order
The following is probably not the easiest way but the code
const h = murmurHash(buf, "buffer", 0xc58f1a7b);
// console.log(parseInt(h, 16).toString(2));
const fromHashFile = Buffer.alloc(4);
fromHashFile.writeInt32BE(-1274144557);
console.log(fromHashFile);
console.log(h);
console.log(h.readInt32BE());
console.log("hash from hashcodes file: -1274144557");
Prints out the following to console:
<Buffer b4 0e 18 d3>
<Buffer b4 0e 18 d3>
-1274144557
hash from hashcodes file: -1274144557