Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 476
Arsen Avatar asked Dec 25 '15 18:12

Arsen


People also ask

Is there a reversible hash function?

Hash functions are not reversible in general. MD5 is a 128-bit hash, and so it maps any string, no matter how long, into 128 bits. Obviously if you run all strings of length, say, 129 bits, some of them have to hash to the same value.

Is hash function is reversible if key value is known?

Anyone who knows or obtains the secret key can decrypt the ciphertext and read the original input. Hashing functions are not reversible. The output of a hashing function is a fixed-length string of characters called a hash value, digest or simply a hash.

What is the hash of an integer?

The most commonly used method for hashing integers is called modular hashing: we choose the array size M to be prime, and, for any positive integer key k, compute the remainder when dividing k by M. This function is very easy to compute (k % M, in Java), and is effective in dispersing the keys evenly between 0 and M-1.

What is two way hashing?

Encryption is a two-way function where information is scrambled in such a way that it can be unscrambled later. Hashing is a one-way function where data is mapped to a fixed-length value. Hashing is primarily used for authentication.


2 Answers

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.
like image 57
Matt Timmermans Avatar answered Oct 20 '22 21:10

Matt Timmermans


You can use any 64-bit block cipher (for example, DES), and use encrypt for a "hash", and decrypt for an "reverse hash".

like image 3
olegarch Avatar answered Oct 20 '22 21:10

olegarch