Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80950fd

Browse files
committed
add 2fa tutorial
1 parent9e60578 commit80950fd

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 1 addition & 0 deletions
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)

‎ethical-hacking/implement-2fa/hotp.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
importpyotp
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+
forotpinrange(4):
17+
print(hotp.verify(input("Enter Code: "),counter))
18+
counter+=1
19+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Program 1: Generate and Save TOTP Key and QR Code
2+
importpyotp
3+
importqrcode
4+
5+
6+
defgenerate_otp_key():
7+
# Generate a random key for TOTP authentication.
8+
returnpyotp.random_base32()
9+
10+
11+
defgenerate_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+
withopen('2fa.txt','w')asf:
25+
f.write(user_key)
26+
# Generate QR Code.
27+
generate_qr_code(user_key,'Muhammad','CodingFleet.com')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Program 2: Verify TOTP Code with Google Authenticator
2+
importpyotp
3+
4+
5+
defsimulate_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+
iftotp.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)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyotp
2+
qrcode

‎ethical-hacking/implement-2fa/totp.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
importpyotp
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)

[8]ページ先頭

©2009-2025 Movatter.jp