3

I have generate a hash code as

string textBoxVal="Naresh";
int code =textBoxVal.GetHashCode();
textBox2.Text=code.ToString();

It has generated a integer value as -1078339947;

Now I want to get the Original name Naresh with this(-1078339947) hashcode. How can I do this.

Learner
  • 1,286
  • 6
  • 34
  • 57
  • 9
    You can't. That's the point of hashing. – Blorgbeard Apr 18 '13 at 07:24
  • If you want to take a string and generate a unique `int` identifier for it, what you're looking for is a database (of some sort, whether it be something that lives in memory for just the lifetime or your application to a full-blown database system), not a hash code. – Damien_The_Unbeliever Apr 18 '13 at 07:32
  • Would it be possible for you to use a `string instead of an `int` for this purpose? Since you can't do this using a standard hashcode, perhaps you can create an algorithm to generate a unique string code to use instead? (I guess this basically amounts to some form of encryption of the original data, instead of creating a hash from it) – Kjartan Apr 18 '13 at 09:10

2 Answers2

5

For all practical purposes, you can't: there are lot less hash codes than there are strings, so there is more than one original value that would give your same hash code.

Hashing is, in reality, a one-way operation. If someone refers to a reversible hash, this isn't a true hash (because a hash by definition reduces an input set into one of a smaller number of output values). The closest operation to what you describe might be an encryption function - this would allow you to reverse the operation - but this is unlikely to generate as small a number as the 10-digit output in your question.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
3

You just can't do that! A hash code doesn't contains all the necessary informations to convert it back to a string.

There is no even guarantee that GetHashCode() will return the same thing in different environments.

Check out Eric Lippert's blog post Guidelines and rules for GetHashCode

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364