# Step-by-Step Guide to Implement Mid Square Hashing in C++

Mid square hashing is a process of generating a pseudo-random number from a given key value. It is used in a variety of applications like cryptography, password hashing and data validation. This guide will help you get a better understanding of mid square hashing and how to implement it in C++.

## Contents

• What is Mid Square Hashing
• How does Mid Square Hashing work?
• How to Implement Mid Square Hashing in C++
• FAQs

## What is Mid Square Hashing?

Mid Square Hashing is a popular hashing technique used to map a given key word or phrase to a numerical value. It is used in a variety of applications like cryptography, password hashing, and data validation. It is a hash algorithm based on the mid-square technique of generating a pseudo-random number from a given key value.

## How does Mid Square Hashing Work?

Mid Square Hashing is a hash algorithm based on the mid-square technique of generating a pseudo-random number from a given key value. The steps involve:

1. Take the given key value and square it.
2. Extract the center digits of the resultant number as the new generated number.
3. Keep repeating the process in step 2 until the generated number is of a fixed size as per requirement.

## How to Implement Mid Square Hashing in C++

Implementing Mid Square Hashing in C++ involves the following steps:

``````#include<bits/stdc++.h>
using namespace std;
``````

2.Create a function to calculate the square of a number:

``````int calculate_square(int num)
{
return (num*num);
}
``````

3.Create a function to extract the center digits from the generated result:

``````string extract_center(int result, int length)
{
string str_result = to_string(result);
int start_index = str_result.length()/2 - (length/2);
int end_index = start_index + length;
return str_result.substr(start_index, end_index);
}
``````

4.Create a function to generate the hashed value using the given key value:

``````int midSquareHash(int key, int length)
{
int result = calculate_square(key);
while(to_string(result).length() != length)
{
string temp = extract_center(result, length);
result = calculate_square(stoi(temp));
}
return result;
}
``````

5.Test the midSquareHash() to generate the hashed value:

``````int key = 87;
int length = 4;
int hashed_value = midSquareHash(key, length);
``````

In this example, the hashed value generated for the key 87 is 3721.

## FAQs

### Q1: What is the purpose of mid square hashing?

Mid Square hashing is a popular hashing technique used to map a given key word or phrase to a numerical value. It is used in a variety of applications like cryptography, password hashing, and data validation.

### Q2: What is the advantage of mid square hashing?

Mid Square hashing is considered to be a secure way of generating pseudo-random numbers from a given key value. It is also relatively simple and easy to implement.

### Q3: How does mid square hashing work?

Mid Square Hashing works by taking the input key value and squaring it. The center digits of the generated number are then extracted and squared again. This process is repeated until the final generated number is of the desired length.

### Q4: What are the steps required to implement mid square hashing in C++?

The steps required to implement mid square hashing in C++ include:

• Including the necessary header files
• Creating a function to calculate the square of a number

-Creating a function to extract the center digits from the generated result

• Creating a function to generate the hashed value using the given key value
• Testing the midSquareHash() to generate the hashed value

### Q5: Are there any precautions which need to be taken while implementing mid square hashing?

Yes, there are certain precautions which need to be taken while implementing mid square hashing. Firstly, it is important to ensure that the extracted strings used for hashing are of uniform length so as to generate consistent and accurate results. Secondly, it is advisable to use inputs for the hashing algorithm that are at least the same length as the output hashed value. This ensures that all generated numbers have the same length and that the resulting random numbers are unpredictable.