Search code examples
algorithmcombinations

Reversible "hash" function from 64-bit integer to 64-bit integer


What I need is a reversible function that transforms a long (64-bit integer) into another long number, in a way that seems "random" for a user (but actually is deterministic), so that 3 subsequent numbers are transformed into 3 numbers completely different to each other.

It is easy to do it without being reversible, but it turns out pretty difficult when it comes to this part.

Basically it's the same question as Reversible hash function?, but I need more than 2^32 distinct values.

Any ideas?

PS: I'm going to write it in Java, but the question itself is pretty generic.


Solution

  • These are the basic requirements for a block cipher, which is usually implemented with a Feistel structure: https://en.wikipedia.org/wiki/Feistel_cipher

    1. Create a hash of the lower 32 bits and XOR it into the upper 32 bits
    2. Swap the lower and upper 32 bits
    3. Repeat a few times.