Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I be sure the built-in hash for a given string is always the same?

Tags:

string

c#

.net

hash

I am getting a string hash like this:

string content = "a very long string";
int contentHash = content.GetHashCode();

I am then storing the hash into a dictionary as key mapping to another ID. This is useful so I don't have to compare big strings during default dictionary hash computation but I can just fish the ID from the dictionary by key.

Can I be sure that the hash for a given string ("a very long string") will be always the same?

Can I be sure that two different strings won't have the same hash?

Also, if possible, how likely is it to get the same hash for different strings?

like image 611
JohnIdol Avatar asked Jan 22 '09 11:01

JohnIdol


People also ask

Is the hash of a string always the same?

What is Hashing? Hashing is simply passing some data through a formula that produces a result, called a hash. That hash is usually a string of characters and the hashes generated by a formula are always the same length, regardless of how much data you feed into it.

Does hash always return the same value?

No matter how large or how small the message, it's always going to return an output that is the same size. Remember, hash algorithms are deterministic, so this means that they always result in the same size output regardless of the size of the input.

Will sha256 always be the same?

It is deterministic, meaning that a specific input (or file) will always deliver the same hash value (number string). This means that it is easy to verify the authenticity of a file. If two people independently (and correctly) check the hash value of a file, they will always get the same answer.

Can hash values be the same?

No two data can theoretically have same Hash Value. There is a condition called as Collision in Hashing. Collision is a situation when two different Data have the same Hash Value. Best hashing algorithm is the one which cannot cause Hash Value Collision.


2 Answers

Yes, it will be consistent since strings are immutable. However, I think you're misusing the dictionary. You should let the dictionary take the hash of the string for you by using the string as the key. Hashes are not guaranteed to be unique, so you may overwrite one key with another.

like image 171
Kent Boogaart Avatar answered Oct 10 '22 10:10

Kent Boogaart


Just to add some detail as to where the idea of a changing hashcode may have come from.

As the other answers have rightly said the hashcode for a specific string will always be the same for a specific runtime version. There is no guarantee that a newer runtime might use a different algorithm perhaps for performance reasons.

The String class overrides the default GetHashCode implementation in object.

The default implementation for a reference type in .NET is to allocate a sequential ID (held internally by .NET) and assign it to the object (the objects heap storage has slot for storing this hashcode, it only assigned on the first call to GetHashCode for that object).

Hence creating an instance of a class, assigning it some values then retrieving the hashcode, followed by doing the exact same sequence with the same set of values will yeild different hashcodes. This may be the reason why some have been led to believe that hashcodes can change. In fact though its the instance of a class which is allocated a hashcode once allocated that hashcode does not change for that instance.

Edit: I've just noticed that none of the answers directly reference each of you questions (although I think the answer to them is clear) but just to tidy up:-

Can I be sure that the hash for a given string ("a very long string") will be always the same?

In your usage, yes.

Can I be sure that two different strings won't have the same hash?

No. Two different strings may have the same hash.

Also, if possible, how likely is it to get the same hash for different strings?

The probability is quite low, resulting hash is pretty random from a 4G domain.

like image 41
AnthonyWJones Avatar answered Oct 10 '22 11:10

AnthonyWJones