Is there a gentle hash function tutorial?

0 votes
asked Sep 27, 2010 by frank

Embarrassingly, picking a hash function (say, for hashing strings, or sets of integers, etc.) is still magic to me: take some prime numbers here, magic constants there, do some bit shifting, modulo something, and done.

Is there a nice, gentle and approachable tutorial about creating hash functions?

3 Answers

0 votes
answered Sep 27, 2010 by edward-leno

You can find a decent, easy hash tutorial at Hash Table Tutorial (discusses hash functions as well). Note that if you do a web search, you can find a lot of good information.

Wikipedia has some basic info on both Hash Tables and Hash Functions.


A similar question was previously asked: Which Hash Function Should I Choose. The question and answers are excellent.

0 votes
answered Sep 5, 2013 by websprinter

It is curious how hard it is to find a basic explanation of hash algorithms. Maybe the topic is so difficult that it's not easy to make a basic tutorial. I was looking for one myself and ran into the same problem.

But you can try this page. What is cool about it is that after you read through the page, at the bottom there's a text box. If you add text to that box and submit the form, the result is a step-by-step listing of how it hashes the input text.

Good luck. If you find anything better, it would be really helpful if you posted it here.

0 votes
answered Sep 15, 2017 by toing

I found this link a bit helpful. It gives a basic over-view but falls short of a thorough understanding of things such as why prime, why bit shift etc..

From this link, highlight some section that gave an overview

What Makes A Good Hash Function Most good hashing functions work by computing the remainder after dividing by the table size N.

This always gives a value between 0 and N-1 so it suitable but if N is a prime number then it is also excellent at scattering the data round the table. Of course, if you have a text value that you want to hash you first have to convert it into a suitable numeric value and a simple scheme like the one in the example will not do.

You need to produce a different numeric value for every possible text value and adding together the ASCII codes of the first two letters clearly doesn't work. A better method is to weight each of the ASCII codes by the position of the letter by multiplying by 1 for the first character, 10 for the second, 100 for the third and so on.. before adding them up to give a single value.

In general building a really good hash function is difficult and in most cases you need to find one that has good properties and has been well tested.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter