Hash Functions and Types of Hash functions
Last Updated :
10 Mar, 2025
Hash functions are a fundamental concept in computer science and play a crucial role in various applications such as data storage, retrieval, and cryptography. A hash function creates a mapping from an input key to an index in hash table. Below are few examples.
- Phone numbers as input keys : Consider a hash table of size 100. A simple example hash function is to consider the last two digits of phone numbers so that we have valid hash table indexes as output. This is mainly taking remainder when input phone number is divided by 100. Please note that taking first two digits of a phone number would not be a good idea for a hash function as there would be many phone number having same first two digits.
- Lowercase English Strings as Keys : Consider a hash table of size 100. A simple way to hash the strings would be add their codes (1 for a, 2 for b, ... 26 for z) and take remainder of the sum when divided by 100. This hash function may not be a good idea as strings "ad" and "bc" would have the same hash value. A better idea would be to do weighted sum of characters and then find remainder. Please refer an example string hashing function for details.
Key Properties of Hash Functions
- Deterministic: A hash function must consistently produce the same output for the same input.
- Fixed Output Size: The output of a hash function should have a fixed size, regardless of the size of the input.
- Efficiency: The hash function should be able to process input quickly.
- Uniformity: The hash function should distribute the hash values uniformly across the output space to avoid clustering.
- Pre-image Resistance: It should be computationally infeasible to reverse the hash function, i.e., to find the original input given a hash value.
- Collision Resistance: It should be difficult to find two different inputs that produce the same hash value.
- Avalanche Effect: A small change in the input should produce a significantly different hash value.
Applications of Hash Functions
- Hash Tables: The most common use of hash functions in DSA is in hash tables, which provide an efficient way to store and retrieve data.
- Data Integrity: Hash functions are used to ensure the integrity of data by generating checksums.
- Cryptography: In cryptographic applications, hash functions are used to create secure hash algorithms like SHA-256.
- Data Structures: Hash functions are utilized in various data structures such as Bloom filters and hash sets.
Types of Hash Functions
There are many hash functions that use numeric or alphanumeric keys. This article focuses on discussing different hash functions:
- Division Method.
- Multiplication Method
- Mid-Square Method
- Folding Method
- Cryptographic Hash Functions
- Universal Hashing
- Perfect Hashing
Let's begin discussing these methods in detail.
1. Division Method
The division method involves dividing the key by a prime number and using the remainder as the hash value.
h(k)=k mod m
Where k is the key and ?m is a prime number.
Advantages:
- Simple to implement.
- Works well when ?m is a prime number.
Disadvantages:
- Poor distribution if ?m is not chosen wisely.
2. Multiplication Method
In the multiplication method, a constant ?A (0 < A < 1) is used to multiply the key. The fractional part of the product is then multiplied by ?m to get the hash value.
h(k)=⌊m(kAmod1)⌋
Where ⌊ ⌋ denotes the floor function.
Advantages:
- Less sensitive to the choice of ?m.
Disadvantages:
- More complex than the division method.
3. Mid-Square Method
In the mid-square method, the key is squared, and the middle digits of the result are taken as the hash value.
Steps:
- Square the key.
- Extract the middle digits of the squared value.
Advantages:
- Produces a good distribution of hash values.
Disadvantages:
- May require more computational effort.
4. Folding Method
The folding method involves dividing the key into equal parts, summing the parts, and then taking the modulo with respect to ?m.
Steps:
- Divide the key into parts.
- Sum the parts.
- Take the modulo ?m of the sum.
Advantages:
- Simple and easy to implement.
Disadvantages:
- Depends on the choice of partitioning scheme.
5. Cryptographic Hash Functions
Cryptographic hash functions are designed to be secure and are used in cryptography. Examples include MD5, SHA-1, and SHA-256.
Characteristics:
- Pre-image resistance.
- Second pre-image resistance.
- Collision resistance.
Advantages:
Disadvantages:
- Computationally intensive.
6. Universal Hashing
Universal hashing uses a family of hash functions to minimize the chance of collision for any given set of inputs.
h(k)=((aâ‹…k+b)modp)modm
Where a and b are randomly chosen constants, p is a prime number greater than m, and k is the key.
Advantages:
- Reduces the probability of collisions.
Disadvantages:
- Requires more computation and storage.
7. Perfect Hashing
Perfect hashing aims to create a collision-free hash function for a static set of keys. It guarantees that no two keys will hash to the same value.
Types:
- Minimal Perfect Hashing: Ensures that the range of the hash function is equal to the number of keys.
- Non-minimal Perfect Hashing: The range may be larger than the number of keys.
Advantages:
Disadvantages:
Conclusion
In conclusion, hash functions are very important tools that help store and find data quickly. Knowing the different types of hash functions and how to use them correctly is key to making software work better and more securely. By choosing the right hash function for the job, developers can greatly improve the efficiency and reliability of their systems.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read