Install PyOTP via pip
mainInstall the PyOTP library using pip to start generating and verifying one-time passwords.
pip install pyotprepository·main·Indexed 25 days ago
https://github.com/pyauth/pyotpA Python library for generating and verifying one-time passwords (OTP) following RFC 4226 (HOTP) and RFC 6238 (TOTP) standards. It provides tools for implementing two-factor (2FA) and multi-factor (MFA) authentication, including support for provisioning URIs for QR codes, secure secret generation via random_base32 and random_hex, and a specialized third-party implementation for Steam TOTP.
Install the PyOTP library using pip to start generating and verifying one-time passwords.
pip install pyotpImplement TOTP (RFC 6238) using the pyotp.TOTP class. You can generate the current code using .now() and verify a provided code using .verify(code). Note that TOTP codes are time-sensitive.
import pyotp
import time
totp = pyotp.TOTP('base32secret3232')
totp.now() # => '492039'
# OTP verified for current time
totp.verify('492039') # => True
time.sleep(30)
totp.verify('492039') # => FalseImplement HOTP (RFC 4226) using the pyotp.HOTP class. You can generate a code for a specific counter index using .at(counter) and verify a code against a specific counter using .verify(code, counter).
import pyotp
hotp = pyotp.HOTP('base32secret3232')
hotp.at(0) # => '260182'
hotp.at(1) # => '055283'
hotp.at(1401) # => '316439'
# OTP verified with a counter
hotp.verify('316439', 1401) # => True
hotp.verify('316439', 1402) # => FalsePyOTP provides helper functions to generate secure secrets:
pyotp.random_base32(): Generates a 32-character base32 secret (compatible with Google Authenticator).pyotp.random_hex(): Generates a 40-character hex-encoded secret.To allow users to easily add credentials to apps like Google Authenticator or Authy, generate a provisioning URI using the .provisioning_uri() method. This URI can then be converted into a QR code.
For TOTP:
pyotp.totp.TOTP(secret).provisioning_uri(name='user@email.com', issuer_name='App Name')
For HOTP:
pyotp.hotp.HOTP(secret).provisioning_uri(name='user@email.com', issuer_name='App Name', initial_count=0)
otpauth:// URI using pyotp.parse_uri(uri).pyotp.contrib.Steam(). It uses the same API as pyotp.TOTP().interval attribute and the current timestamp.random_base32 to generate a random Base32 encoded string for use as an OTP secret. Note that the otpauth scheme does not use Base32 padding for secret lengths not divisible by 8, which may cause issues with some third-party tools. The length must be at least 32 characters to ensure the secret is at least 160 bits.TOTP instance by providing a base32 encoded secret. You can customize the number of digits, the HMAC digest function (defaults to hashlib.sha1), the account name, the issuer, and the time interval in seconds.verify(). You can specify a for_time and a valid_window. The valid_window extends validity to a specific number of counter ticks before and after the current one to account for clock drift.The Steam class provides a specialized implementation of Time-based One-Time Password (TOTP) specifically for Steam accounts. It uses Steam's custom alphabet (23456789BCDFGHJKMNPQRTVWXY) and defaults to a 5-digit code length.
To use it, instantiate the Steam class with your base32 secret. You can optionally provide a name, issuer, or a custom interval (defaulting to 30 seconds).