robertklep / node-metrohash

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

Library is not behave same as i used with python #21

Closed stefanmilic closed 3 years ago

stefanmilic commented 3 years ago

This is small python code snippet:
import metrohash uuid = b'E7A636A9-6ACC-5D83-ABD7-53B8B6DFEF3B' h = metrohash.metrohash64(uuid)

as result i got bytes b'm\x85\xee%L1\xe2\x89

But when I used the javascript package

const metrohash64 = require('metrohash').metrohash64; let digest = metrohash64('E7A636A9-6ACC-5D83-ABD7-53B8B6DFEF3B'); console.log(digest) // 6d85ee254c31e289 i got hex value. Looks like we skip the step where the initial value converts to bytes, which is very important for me.

Do you know how can I get that?

robertklep commented 3 years ago

You can convert a hex string to a Buffer (containing bytes) like this:

let digest = metrohash64('E7A636A9-6ACC-5D83-ABD7-53B8B6DFEF3B');
let buffer = Buffer.from(digest, 'hex');
stefanmilic commented 3 years ago

Look this is python code :

import metrohash uuid = b'E7A636A9-6ACC-5D83-ABD7-53B8B6DFEF3B' h = metrohash.metrohash64(uuid) int(h[::-1].hex(), 16)

and as result, i got 9935557931056399725 integer

by steps :

h : b'm\x85\xee%L1\xe2\x89'

h[::-1]. b'\x89\xe21L%\xee\x85m'

h[::-1].hex(). 89e2314c25ee856d

init(89e2314c25ee856d, 16) // 9935557931056399725

I should do the same thing in Javascript( node)

robertklep commented 3 years ago

To get the same result in a recent version of Node.js, you can do something like this:

let digest = metrohash64('E7A636A9-6ACC-5D83-ABD7-53B8B6DFEF3B');
let buffer = Buffer.from(digest, 'hex').swap64();
let result = buffer.readBigUInt64BE(); // 9935557931056399725n

Be aware that result will be a BigInt because native Javascript integers are limited in size.

stefanmilic commented 3 years ago

Yes, that might work. Thanks a lot ;)