|
| 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') |