4

I need use hash and en-hashed (two-way) function for strings. Hash should be fixed length (5-6 symbols).

Example:

String hashed = MagicHashLib.hash("long string"); //hash is hd45dk as an example
String enhashedLongString = MagicHashLib.enhash(hash);

Does exist already implemented algorithm in Java? Or I should write my own?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Aureum
  • 199
  • 10

2 Answers2

2

What you are looking for is called encryption and decryption.

Hashing is always one-way. You cannot feasibly recover original string from its hashed value.

For brief explanation about differences between hashing and encryption see this answer

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
1

I don't think such hash exists, regardless of the programming language.

Here is a quote from the hash function wikipedia page:

A hash function is any function that can be used to map data of arbitrary size to data of fixed size.

It means that you have a fixed set of values, e.g. a 6 character long hash can store 16^6 different values. However, you can create the hash value of any string. So a hash function maps potentially infinite different values to a fixed sets of values. Hence there will be collisions. So you cannot tell just by the hash value which was the right original value.

A simplistic example from an imaginary hash function: John maps to abc. Jane maps to ghi. Doe also maps to abc. So when you see abc as a hashed value, where did that come from? Was it John? Was it Doe?

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • 1
    Thanks for pointing it out! Changed to `16^6` because that's the usual radix in hashes. But it's a matter of radix anyway. You can include other chars as &, #. But that would be weird :) – Tamas Rev Jun 01 '18 at 09:19
  • 1
    I got myself a bit confused here. You're probably right :) – Lino Jun 01 '18 at 09:20