Agenda
- Hash
- Hash Collision
- Rehashing
- Chaining
- Algorithm
- Application
Hash
Hash is most common used data structure in every day programming. If you are python developer every time you use dict or set, it internally uses hashing. In perl you call it as hash using {}. Or HashSet or HashMap in Java.
There are different ways to save data, for example in a variable, arrays, structures and so on. Hash is yet another way to save data. To say the most simple reason is if we have key and value pair we can use hash for quick save and retrieval.
There are three perquisites for hashing, given below:
- key
- value
- hash function
- Key: Unique data to store the value
- Value: Data which needs to be associated with the Key
Hash Function: Function used compute the hash code from the given key. The given hash code will be the index for us to save value. For each key the hash function is expected to give us unique hash code(index).
Fig 1: Explains each key goes through hash function and based on its output index values are saved.
Credits: https://www.tutorialspoint.com/data_structures_algorithms/hash_data_structure.htm
Hash Collision
Hash Collision is a flaw of the hash function which generates same hash code for two different keys.
Eg: We have two keys k1 and k2, when the h(k1) and h(k2) generates same hash code, how will the data stored and retrieved.
A good hash function is one that minimizes the collisions and spreads the records uniformly throughout the table.
There are two basic techniques to resolve this, rehashing and chaining.
Rehashing
The simplest method to place the record in the next available position of the array. This techniques is also called linear probing.
If array location h(key) is already occupied by a record. A general rehash(rh) function is called rh(h(key)). If it is already occupied then again rh(rh(h(key))) is called.
However it is possible for the rehash function to called in a loop indefinitely, even if there are many empty functions.
Chaining
Another method of resolving the hash clashes is called chaining. It involves keeping the distinct linked list for all the records whose keys hash to the particular value.
Below find the nice representation of chained hash table, credits to wikipedia.
Fig 2: A small phone book as hash table using chaining mechanism
Credits : https://en.wikipedia.org/wiki/Hash_table
Algorithm
- Create an array of structure
- Define a hash function to accept key as input and return the index. Note: There are several ways to define the hash function, we have chose the basic one.
- Search function
- Get the index by calling the hash function passing the key
- Validate if the index exists
- If exists loop through the index
- Match the given in each of the chain structure pointed to the index
- If found return the structure address pointer
- Else return NULL
- Insert function
- Call the Search function passing the key to determine whether the given key is already present
- If it is present, override with the new value
- Else create a new entry
- Return NULL if there is no memory for the new entry
Source code:
Note: Chaining mechanism is used above to avoid hash collisions
Application
Now that we have seen about hash and its implementation, lets see its common applications.
In Cryptography hash functions are used to produce the hashed output from the input, which is impossible to reverse from output to input.
Password verification is another important usage of cryptography hash function. When you enter the password, from the client the password is hashed and sent to server. As the server already knows the hash of your password it can simply compare it. If there is an middleman attack your password is still unknown to the hacker.
Rabin Karp string search algorithm, uses hashing to find set of patterns in the string. And the real world application of it is to detect plagiarism.
Reference and Credits:
K&R C Programming Language
Practical Cryptography for DevelopersHash Functions - Practical Cryptography for Developers
Original Blog Posted in OSFY
https://www.opensourceforu.com/2021/10/working-with-hash-in-c/
For further research and updates maintaining the blog here.



