3

I think I can use Cantor's to create a unique hash n = ((x + y)*(x + y) + x - y)/2...

but can I reverse this hash? And if not, can someone provide a similar formula pair for a reversible hash?

Thanks.

Jeff
  • 32,948
  • 13
  • 96
  • 198
  • 1
    hashes are generally not reversible by definition... – Mitch Wheat Nov 15 '11 at 04:50
  • Why exactly do you need to hash 8 bytes of data? That's well into the "so small you may as well just use it" realm, particularly if you would've used a reversible hash (no security). – ssube Nov 15 '11 at 04:51
  • I need to generate a unique id from integer ids from two different data sources. The ids from the two different data sources may overlap, so I can't just use the ids by themselves. I also need a way of decomposing the unique id that I create. – Jeff Nov 15 '11 at 04:53
  • Are you trying to compress two arbitrary numbers into one number of the same size with no loss of information? – Andrew Shepherd Nov 15 '11 at 04:53
  • Yes, exactly, that's what I'm trying to do. – Jeff Nov 15 '11 at 05:32
  • possible duplicate of [Mapping two integers to one, in a unique and deterministic way](http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way) – thrau Apr 14 '14 at 08:16

3 Answers3

3

if x and y and n are all the same data types.

n = ((x + y)*(x + y) + x - y)/2...

when x and y are near the datatype::max n will overflow and you will lose information and not be able to recover x and y.

If on the other hand if x and y are always within a range let say 0-FOO

n = Foo * x + y 

can be a recoverable hash provided again n has not overflown

   n % Foo

will give you y. Once y is known (n-y)/Foo will give you x

parapura rajkumar
  • 22,951
  • 1
  • 49
  • 82
  • Caveat: Foo must be at least 1 larger than y, otherwise you got a corner case problem. –  Nov 15 '11 at 05:28
  • Thanks - I'm not clear on how to calculate foo though... Can you elaborate please? – Jeff Nov 15 '11 at 05:34
  • Never mind, you're just saying FOO * (x + y) is a sufficient way of getting a unique value, right? – Jeff Nov 15 '11 at 05:45
  • 1
    No.. Foo*x + y. Just Image x,y is between 1-99. If You pick FOO as 100 and you if x = 23... and y = 42... your has becomes 2342, enough information that can be easily recovered – parapura rajkumar Nov 15 '11 at 11:48
  • @bdare... Good corner catch...I think there is a 20 second window on stackoverflow before which you get to post answers... – parapura rajkumar Nov 15 '11 at 11:49
2

Here's a unique, reversible, and completely impractical hash for two integers:

for two numbers x and y, return the product of the xth and yth prime numbers. This is unique and reversible if you assume the order does not matter. If they do, then you can add a character denoting which of the two prime factors of your "hash" is x or y.

Note that your hash space is larger than your input space. Oh well. That's what you get for trying to store more than one bit in a bit.

0

You should specify if you have control over the upper bound of inputs, and if so what size your inputs are, what size your output should be, if inputs and output are signed or not etc (and of course if function should be reversible or not). A good answer would depend on all that. In this link you find a good solution to encode two positive numbers uniquely and decode them back.

nawfal
  • 62,042
  • 48
  • 302
  • 339