2

I am trying to generate same 64bit integers using both XQuery and Java. Currently, I am using XQuery (MarkLogic) and using the xdmp:hash64 function to generate a 64bit number.

For example, xdmp:hash64('123') will give us 6570863421667228143.

Now i would like to generate the same in Java. I was wondering if someone could provide some insights

Thank you

dirkk
  • 6,160
  • 5
  • 33
  • 51
digerati
  • 85
  • 1
  • 5
  • @dirkk: The Q ask not about some random hash method but about one which delivers *exactly* the same result as `xdmp:hash64`. Hence a pointer to generic string hashing in Java is futile. And for the same reason this is not a duplicate. – A.H. Aug 23 '14 at 09:54
  • @A.H. You are correct. In this case, as ML is not open source and the algorithm isn't public, you can't really know. Either you ask and they tell you or you could try different well-known hashing algorithms (and mask to 64bit) and see if it fits. However, the easiest is probably to use in XQuery also your own hashing function and simply implement the same in Java. – dirkk Aug 23 '14 at 15:59

1 Answers1

0

The xdmp:hash64 algorithm is proprietary and has unknown properties. You could send every input from Java to MarkLogic, to ask it what the hash is. But I'd look for a hash algorithm that's available in both environments.

Depending on your needs, SHA-2 may be a suitable choice: https://docs.marklogic.com/xdmp:sha512 or xdmp:sha384 or xdmp:sha256.

You asked for a 64-bit number like xdmp:hash64 produces: 64-bit unsigned longs. The SHA-2 functions return hex or base64 strings, but hex is easy to work with:

xdmp:hex-to-integer(xdmp:sha512('fubar'))

If that throws XDMP-FIPSWEAKHASH then your cluster is in FIPS mode. In that case you have to use the HMAC variant, by adding a secret key:

xdmp:hex-to-integer(xdmp:hmac-sha512('s3cr!t', 'fubar'))
=> 18446744073709551615

For equivalent Java code SHA2 password hashing in java may help. As long as you use the same algorithm and key on both sides you should be fine.

Note that working with unsigned 64-bit long integers may not be ideal for Java, because Java doesn't have unsigned 64-bit longs as a primitive type. I understand JDK8 offers some help. But comparing hex strings might turn out to be as easy and efficient as any other solution.

Community
  • 1
  • 1
mblakele
  • 7,782
  • 27
  • 45