Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

Create Bcrypt Hashing Module.md

Abdul Wahab Junaidbbf3df45a816
diff --git a/Cryptography/Cryptography Course/Course/Bcrypt Hashing Module.md b/Cryptography/Cryptography Course/Course/Bcrypt Hashing Module.md
new file mode 100644
index 0000000..a270f7f
--- /dev/null
+++ b/Cryptography/Cryptography Course/Course/Bcrypt Hashing Module.md
@@ -0,0 +1,123 @@
+### **bcrypt Hashing Module in Python**
+
+**bcrypt** is a modern password hashing library designed to be slow, making brute-force attacks more difficult. It’s widely used for securely hashing passwords, and its primary features include:
+
+1. **Salting**: bcrypt automatically generates a unique salt for each password hash, preventing rainbow table attacks.
+2. **Key Stretching**: bcrypt uses multiple rounds of hashing (work factor), making it more resistant to brute-force attacks.
+3. **Cross-Platform**: The bcrypt algorithm works across various platforms and is available in multiple programming languages.
+
+### **How to Use bcrypt in Python**
+
+To use bcrypt in Python, you first need to install the `bcrypt` package. This can be done using `pip`:
+
+```bash
+pip install bcrypt
+```
+
+### **Steps to Hash and Verify Password with bcrypt**
+
+#### **1. Hashing a Password**
+
+Here's how to hash a password using bcrypt in Python:
+
+```python
+import bcrypt
+
+# Step 1: The password to hash
+password = "my_secure_password".encode('utf-8') # Encoding password to bytes
+
+# Step 2: Generate a salt (bcrypt automatically does this)
+salt = bcrypt.gensalt()
+
+# Step 3: Hash the password with the salt
+hashed_password = bcrypt.hashpw(password, salt)
+
+print("Salt:", salt)
+print("Hashed Password:", hashed_password)
+```
+
+In this example:
+- The `bcrypt.gensalt()` function generates a new salt automatically.
+- The `bcrypt.hashpw()` function hashes the password with the salt.
+
+#### **2. Verifying a Password**
+
+To verify a password, we compare the stored hash with the hash of the entered password:
+
+```python
+import bcrypt
+
+# Step 1: Store the hash and salt (in practice, you'd store these in a database)
+stored_hash = b"$2b$12$KMoeP2pz6MPZ5YVUtNfWDu1hF9l8e/ve5kocJgZn9QNhQGeT3mrOe" # Example bcrypt hash
+
+# Step 2: Password entered by the user for verification
+entered_password = "my_secure_password".encode('utf-8')
+
+# Step 3: Check if the entered password, when hashed, matches the stored hash
+if bcrypt.checkpw(entered_password, stored_hash):
+ print("Password is correct")
+else:
+ print("Password is incorrect")
+```
+
+In this example:
+- `bcrypt.checkpw()` is used to compare the entered password with the stored hash. It hashes the entered password and checks if it matches the stored hash.
+
+#### **3. Adjusting the Work Factor (Cost Factor)**
+
+The work factor, or cost factor, determines how many rounds of hashing bcrypt should use. This can be adjusted when generating a salt. The higher the work factor, the slower the hashing process, making brute-force attacks more difficult.
+
+```python
+import bcrypt
+
+# Example of a higher cost factor (12 rounds)
+salt = bcrypt.gensalt(rounds=12) # Default is 10 rounds
+hashed_password = bcrypt.hashpw("my_secure_password".encode('utf-8'), salt)
+
+print("Salt:", salt)
+print("Hashed Password:", hashed_password)
+```
+
+**Note**: Increasing the rounds increases the time it takes to compute the hash, which is a good security feature, but it also means it will take longer to verify passwords.
+
+### **Why Use bcrypt?**
+
+- **Security**: bcrypt is designed to be computationally expensive, making brute-force and rainbow table attacks much more difficult.
+- **Automatic Salting**: bcrypt automatically generates a unique salt for each password, preventing the use of precomputed tables.
+- **Flexible Work Factor**: The ability to increase the number of rounds (cost factor) ensures that bcrypt can be made slower as hardware capabilities improve, maintaining security over time.
+
+### **Example: Full Workflow of bcrypt Hashing and Verification**
+
+```python
+import bcrypt
+
+# User-provided password
+password = "my_super_secure_password".encode('utf-8')
+
+# Generate a salt
+salt = bcrypt.gensalt(rounds=12) # 12 rounds for a higher cost factor
+
+# Hash the password with the salt
+hashed_password = bcrypt.hashpw(password, salt)
+
+print("Hashed Password:", hashed_password)
+
+# User re-enters password for verification
+entered_password = "my_super_secure_password".encode('utf-8')
+
+# Verify if the entered password matches the stored hash
+if bcrypt.checkpw(entered_password, hashed_password):
+ print("Password is correct")
+else:
+ print("Password is incorrect")
+```
+
+### **Security Considerations**
+
+1. **Never store plaintext passwords**: Always hash passwords before storing them.
+2. **Unique salt for each password**: bcrypt generates a unique salt for every password, so even if two users have the same password, their hashed values will be different.
+3. **Adjust work factor based on your application needs**: Consider balancing the security of a higher work factor with the computational overhead it introduces. Too high of a work factor might impact performance for high-traffic systems.
+
+### **Conclusion**
+
+bcrypt is a robust, secure, and widely adopted algorithm for password hashing in Python. Its combination of salt generation and a tunable work factor makes it a reliable choice for secure password storage, protecting against both rainbow table attacks and brute-force attacks. It’s essential to incorporate bcrypt in any system that handles sensitive user authentication data.