OTP Password Verification

Code Properties

  • Language: Python
  • Modules: smtplib, random

Overview

Simple OTP (One-Time Password) generation and verification system using email delivery.

Code

import math
import random
import smtplib
 
# generate 6-digit OTP
digits = "0123456789"
OTP = ""
for i in range(6):
    OTP += digits[math.floor(random.random() * 10)]
 
# send OTP via email
msg = f"{OTP} is your OTP"
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login("your_email@gmail.com", "your_app_password")
 
email_id = input("Enter your email: ")
s.sendmail('sender@example.com', email_id, msg)
 
# verify OTP
user_input = input("Enter Your OTP: ")
if user_input == OTP:
    print("Verified")
else:
    print("Please check your OTP again")

Usage

# for production use, consider using secrets module
import secrets
 
def generate_otp(length=6):
    """Generate a secure OTP."""
    return ''.join(secrets.choice('0123456789') for _ in range(length))
 
otp = generate_otp()

Warning

For production use, use app-specific passwords and secure credential storage.


Appendix

Note created on 2024-04-23 and last modified on 2024-12-31.

See Also


(c) No Clocks, LLC | 2024