Java recreates a string from hashcode

Is it possible to use the hash code of a string in java and recreate this string?

eg. something like that:

String myNewstring = StringUtils.createFromHashCode("Hello World".hashCode()); if (!myNewstring.equals("Hello World")) System.out.println("Hmm, something went wrong: " + myNewstring); 

I say this because I have to turn the string into an integer value and restore this string from this integer value.

+6
source share
5 answers

It's impossible. The hash code for String is lossy; many String values ​​will result in the same hash code. An integer has 32 bit positions, and each position has two values. It is impossible to match even 32-character strings (for example) (each character has many possibilities) into 32 bits without collisions. They just don't fit.

If you want to use arbitrary precision arithmetic (say BigInteger), then you can just take each character as an integer and combine them all together. Voila.

+5
source

Not. Multiple lines can have the same hash code. Theoretically, you can create all the lines that have this hash code, but it will be almost infinite.

+4
source

Impossible, I'm afraid. Think about it, hashcode is a long value, i.e. 8 bytes. A string may be smaller than this, but it can also be much longer; you cannot compress a long string into 8 bytes without losing anything.

The hashcode Java algorithm sums every 8th byte, if I remember correctly that you lose 7 out of 8 bytes. If your lines are very short, you can encode them as int or long without losing anything.

+2
source

Suppose a line consists only of letters, numbers and punctuation marks, so there are about 70 possible characters.

log_70{2^32} = 5.22...

This means that for any given integer, you will find a 5- or 6-digit string with this as your hash code. So, extracting "Hello World" : impossible; but "Hello" may work if you are lucky.

0
source

For example, "1019744689" and "123926772" have a hash code of -1727003481. This proves that for any integer you can get a different result (i.e. reversehashcode(hashcode(string)) != string ).

0
source

Source: https://habr.com/ru/post/890429/


All Articles