Skip to content

Commit 80950fd

Browse files
committed
add 2fa tutorial
1 parent 9e60578 commit 80950fd

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5959
- [How to Generate Fake User Data in Python](https://thepythoncode.com/article/generate-fake-user-data-in-python). ([code](ethical-hacking/fake-user-data-generator))
6060
- [Bluetooth Device Scanning in Python](https://thepythoncode.com/article/build-a-bluetooth-scanner-in-python). ([code](ethical-hacking/bluetooth-scanner))
6161
- [How to Create A Fork Bomb in Python](https://thepythoncode.com/article/make-a-fork-bomb-in-python). ([code](ethical-hacking/fork-bomb))
62+
- [How to Implement 2FA in Python](https://thepythoncode.com/article/implement-2fa-in-python). ([code](ethical-hacking/implement-2fa))
6263

6364
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
6465
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

Diff for: ethical-hacking/implement-2fa/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Implement 2FA in Python](https://thepythoncode.com/article/implement-2fa-in-python)

Diff for: ethical-hacking/implement-2fa/hotp.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pyotp
2+
3+
# Set the key. A variable this time
4+
key = 'Muhammad'
5+
# Make a HMAC-based OTP
6+
hotp = pyotp.HOTP(key)
7+
8+
# Print results
9+
print(hotp.at(0))
10+
print(hotp.at(1))
11+
print(hotp.at(2))
12+
print(hotp.at(3))
13+
14+
# Set counter
15+
counter = 0
16+
for otp in range(4):
17+
print(hotp.verify(input("Enter Code: "), counter))
18+
counter += 1
19+

Diff for: ethical-hacking/implement-2fa/otp_qrcode_and_key.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Program 1: Generate and Save TOTP Key and QR Code
2+
import pyotp
3+
import qrcode
4+
5+
6+
def generate_otp_key():
7+
# Generate a random key for TOTP authentication.
8+
return pyotp.random_base32()
9+
10+
11+
def generate_qr_code(key, account_name, issuer_name):
12+
# Generate a QR code for TOTP authentication.
13+
uri = pyotp.totp.TOTP(key).provisioning_uri(name=account_name, issuer_name=issuer_name)
14+
img = qrcode.make(uri)
15+
img.save('totp_qr.png')
16+
print("QR Code generated and saved as 'totp_qr.png'.")
17+
18+
19+
# Main code.
20+
# Generate user key.
21+
user_key = generate_otp_key()
22+
print("Your Two-Factor Authentication Key:", user_key)
23+
# Save key to a file for reference purposes
24+
with open('2fa.txt', 'w') as f:
25+
f.write(user_key)
26+
# Generate QR Code.
27+
generate_qr_code(user_key, 'Muhammad', 'CodingFleet.com')

Diff for: ethical-hacking/implement-2fa/otp_verification.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Program 2: Verify TOTP Code with Google Authenticator
2+
import pyotp
3+
4+
5+
def simulate_authentication(key):
6+
# Simulate the process of authenticating with a TOTP code.
7+
totp = pyotp.TOTP(key)
8+
print("Enter the code from your Google Authenticator app to complete authentication.")
9+
user_input = input("Enter Code: ")
10+
if totp.verify(user_input):
11+
print("Authentication successful!")
12+
else:
13+
print("Authentication failed. Please try again with the right key.")
14+
15+
16+
# Main Code
17+
# The key should be the same one generated and used to create the QR code in Program 1
18+
user_key = open("2fa.txt").read() # Reading the key from the file generated in Program 1 (otp_qrcode_and_key.py)
19+
simulate_authentication(user_key)

Diff for: ethical-hacking/implement-2fa/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyotp
2+
qrcode

Diff for: ethical-hacking/implement-2fa/totp.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pyotp
2+
3+
# Generate a random key. You can also set to a variable e.g key = "CodingFleet"
4+
key = pyotp.random_base32()
5+
# Make Time based OTPs from the key.
6+
totp = pyotp.TOTP(key)
7+
8+
# Print current key.
9+
print(totp.now())
10+
11+
# Enter OTP for verification
12+
input_code = input("Enter your OTP:")
13+
# Verify OTP
14+
print(totp.verify(input_code))

0 commit comments

Comments
 (0)