Skip to content

code (py)

Latest

Choose a tag to compare

@Janhavi1018 Janhavi1018 released this 15 May 13:50
502e353
`import secrets`  # For cryptographically secure random generation

class OTPManager:
    def __init__(self):
        self.current_otp = None  # Holds the current OTP value

    # Generate a secure OTP using the selected character set and desired length
    def generate_otp(self, length=6, alphanumeric=True):
        digits = "0123456789"  # Numeric character set
        alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"  # Alphanumeric set
        charset = alphanum if alphanumeric else digits  # Choose the character set

        # Securely select random characters to create the OTP
        otp_value = ''.join(secrets.choice(charset) for _ in range(length))

        # Store the OTP
        self.current_otp = otp_value
        return otp_value  # Return the generated OTP

    # Return the last generated OTP
    def get_current_otp(self):
        return self.current_otp