Skip to content

Commit a1fa1e1

Browse files
authored
Merge pull request #1584 from dhairyagothi/main
Added HASH documentation
2 parents 3a4606c + bca2bb9 commit a1fa1e1

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

docs/dsa/Hash/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Hash",
3+
"position": 15,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Hash is a data structure that maps keys to values. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found."
7+
}
8+
}

docs/dsa/Hash/hash.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Hash in DSA (Data Structures and Algorithms)
2+
3+
Hash in DSA (Data Structures and Algorithms) is a function that takes an input (or "key") and produces a fixed-size string of characters, which is typically a unique representation of the input. Hash functions are commonly used in various applications, such as data indexing, password storage, and digital signatures.
4+
5+
![alt text](image.png)
6+
7+
- A hash function takes an input and applies a mathematical algorithm to generate a hash value. The hash value is a fixed-size string that is unique to the input data. It is important to note that even a small change in the input data will result in a completely different hash value.
8+
9+
![alt text](image-1.png)
10+
11+
- Hash functions are designed to minimize the occurrence of hash collisions, where two different inputs produce the same hash value. However, it is still possible for collisions to occur due to the limited size of the hash value compared to the potentially infinite input space.
12+
13+
- One common use of hash functions is in data indexing. Hash tables, also known as hash maps, use hash functions to map keys to specific locations in memory, allowing for efficient retrieval and storage of data.
14+
15+
Hash functions are also used in password storage. Instead of storing passwords in plain text, they are hashed and the hash value is stored. When a user enters their password, it is hashed and compared to the stored hash value for authentication.
16+
17+
18+
19+
Hash functions are an integral part of digital signatures. They are used to generate a unique hash value for a document or message, which is then encrypted with the sender's private key. The recipient can verify the integrity of the message by decrypting the hash value with the sender's public key and comparing it to the computed hash value of the received message.
20+
21+
Overall, hash functions play a crucial role in various applications by providing a unique representation of data, enabling efficient data retrieval, ensuring data integrity, and enhancing security.
22+
23+
## Implementing Hash Functions in Different Programming Languages
24+
25+
Here's a basic example of how to implement a hash function in different programming languages:
26+
27+
Python:
28+
```python
29+
import hashlib
30+
31+
def hash_string(input_string):
32+
hash_object = hashlib.sha256(input_string.encode())
33+
return hash_object.hexdigest()
34+
35+
input_string = "Hello, World!"
36+
hashed_string = hash_string(input_string)
37+
print(hashed_string)
38+
```
39+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
40+
41+
Java:
42+
```java
43+
import java.security.MessageDigest;
44+
import java.security.NoSuchAlgorithmException;
45+
46+
public class HashExample {
47+
public static String hashString(String inputString) throws NoSuchAlgorithmException {
48+
MessageDigest md = MessageDigest.getInstance("SHA-256");
49+
byte[] hashBytes = md.digest(inputString.getBytes());
50+
StringBuilder sb = new StringBuilder();
51+
for (byte b : hashBytes) {
52+
sb.append(String.format("%02x", b));
53+
}
54+
return sb.toString();
55+
}
56+
57+
public static void main(String[] args) throws NoSuchAlgorithmException {
58+
String inputString = "Hello, World!";
59+
String hashedString = hashString(inputString);
60+
System.out.println(hashedString);
61+
}
62+
}
63+
```
64+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
65+
66+
C++:
67+
```cpp
68+
#include <iostream>
69+
#include <openssl/sha.h>
70+
71+
std::string hashString(const std::string& inputString) {
72+
unsigned char hash[SHA256_DIGEST_LENGTH];
73+
SHA256_CTX sha256;
74+
SHA256_Init(&sha256);
75+
SHA256_Update(&sha256, inputString.c_str(), inputString.length());
76+
SHA256_Final(hash, &sha256);
77+
std::stringstream ss;
78+
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
79+
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
80+
}
81+
return ss.str();
82+
}
83+
84+
int main() {
85+
std::string inputString = "Hello, World!";
86+
std::string hashedString = hashString(inputString);
87+
std::cout << hashedString << std::endl;
88+
return 0;
89+
}
90+
```
91+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
92+
93+
Please note that these examples use the SHA-256 hash function as an illustration. Different hash functions may be used depending on the specific requirements of your application.
94+
95+
## Example Questions with Code in Python
96+
97+
Here are some example questions with code in Python to help you practice implementing hash functions:
98+
99+
1. **Question:** Write a Python function to calculate the MD5 hash of a given string.
100+
101+
```python
102+
import hashlib
103+
104+
def calculate_md5_hash(input_string):
105+
hash_object = hashlib.md5(input_string.encode())
106+
return hash_object.hexdigest()
107+
108+
input_string = "Hello, World!"
109+
md5_hash = calculate_md5_hash(input_string)
110+
print(md5_hash)
111+
```
112+
113+
**Output:** `3e25960a79dbc69b674cd4ec67a72c62`
114+
115+
2. **Question:** Implement a Python program to find the SHA-1 hash of a file.
116+
117+
```python
118+
import hashlib
119+
120+
def calculate_sha1_hash(file_path):
121+
sha1_hash = hashlib.sha1()
122+
with open(file_path, 'rb') as file:
123+
for chunk in iter(lambda: file.read(4096), b''):
124+
sha1_hash.update(chunk)
125+
return sha1_hash.hexdigest()
126+
127+
file_path = "path/to/file.txt"
128+
sha1_hash = calculate_sha1_hash(file_path)
129+
print(sha1_hash)
130+
```
131+
132+
**Output:** `2ef7bde608ce5404e97d5f042f95f89f1c232871`
133+
134+
3. **Question:** Write a Python function to generate a random salt value for password hashing.
135+
136+
```python
137+
import os
138+
import hashlib
139+
140+
def generate_salt():
141+
salt = os.urandom(16)
142+
return salt.hex()
143+
144+
salt = generate_salt()
145+
print(salt)
146+
```
147+
148+
**Output:** `a5f7b9c8d7e6f5a4b3c2d1e0f9e8d7c6`
149+
150+
Remember to customize these examples based on your specific requirements and use appropriate hash functions for your application.

docs/dsa/Hash/image-1.png

25.3 KB
Loading

docs/dsa/Hash/image.png

55.5 KB
Loading

0 commit comments

Comments
 (0)