# How to test if a hashing algorithm is good? [duplicate]

Possible Duplicate:
Which hashing algorithm is best for uniqueness and speed?

I have created a hash algorithm and would like to test if it is usable. What do I need to test and how?

-

## marked as duplicate by Walter, Glenn Nelson, thorsten müller, GlenH7, StuperUserDec 17 '12 at 16:19

– Yannis Dec 16 '12 at 11:06
Hmm, yes, sorry. Seems like a duplicate indeed! EDIT: I can't close my question because the duplicate is on SO and not on Programmers… – Oltarus Dec 16 '12 at 11:08

Short Answer: It depends on many-many factors.

There is no such thing as a good hash function for universal hashes (although there is such a thing as “universal hashing” but it has different meaning).

Depending on the context different criteria determine the quality of a hash. Some people may thing about SHA, however it is a cryptographic hash and it isn't at all good for hash tables which you probably mean.

Longer Answer: Each hash tables has very specific requirements, and still finding a good hash function universally is hard because different data types expose different information that can be hashed. As a rule of thumb it is good to consider all information a type holds equally. This is not always easy or even possible. For reasons of statistics (and hence collision), it is also important to generate a good spread over the problem space, i.e. all possible objects. This means that when hashing numbers between 100 and 1050 it's no good to let the most significant digit play a big part in the hash because for ~ 90% of the objects, this digit will be 0. It's far more important to let the last three digits determine the hash.

Another important thing when hashing strings is to consider all characters (except when it's known in advance that the first three characters of all strings will be the same: because it would be a waste)

Edit:

1) Just to eliminate the confusion between hashing and encryption look here - Fundamental difference between Hashing and Encryption algorithms

2) Also a good discussion on - What is the difference between a Hash Function and a Cryptographic Hash Function?

References to look:

-
How exactly could a cryptographic hash function be bad for a hash table (except perhaps for execution speed)? It seems to me that the requirements for a cryptographic hash are strictly greater than for a hash function in a hash table. – Michael Borgwardt Dec 16 '12 at 13:07
this sounds to me as a confusion between hashes and cryptographic hash, here is the explanation - security.stackexchange.com/questions/11839/… – Yusubov Dec 16 '12 at 13:21
no confusion at all, I understand the difference completely, but my question stands: what do you mean when you say "SHA, however it is a cryptographic hash and it isn't at all good for hash tables" – Michael Borgwardt Dec 16 '12 at 13:31
digital size and speed was in my mind – Yusubov Dec 16 '12 at 13:34
OK, so I'd say we can agree that for those reasons it would not be ideal, but would do the job well enough. – Michael Borgwardt Dec 16 '12 at 13:59