Turn on two-factor authentication almost anywhere and you will be asked to install Google Authenticator, Microsoft Authenticator or a similar app. It shows a six-digit code that changes every 30 seconds and works even when the phone is offline. How do the site and the phone agree on the same code without talking? The answer is an open standard called TOTP.
A shared secret plus time
TOTP is defined in RFC 6238 and builds on HOTP (RFC 4226). When you enable 2FA, the site generates a random secret and shows it as a QR code; the app stores it, the server keeps a copy. From then on each side splits the current time into 30-second steps, runs the step number and the secret through HMAC and extracts six digits. Same secret, same time, same code. The QR code simply contains a link like this:
otpauth://totp/Misol:ali@misol.uz?secret=JBSWY3DPEHPK3PXP&issuer=Misol&algorithm=SHA1&digits=6&period=30
The secret value is the key itself, so never share the QR code or keep a screenshot of it in a cloud photo album.
The algorithm in a few lines
import base64, hashlib, hmac, struct, time
def totp(secret_b32, step=30, digits=6):
key = base64.b32decode(secret_b32.upper() + '=' * (-len(secret_b32) % 8))
counter = int(time.time()) // step
h = hmac.new(key, struct.pack('>Q', counter), hashlib.sha1).digest()
offset = h[-1] & 0x0F
code = (struct.unpack('>I', h[offset:offset + 4])[0] & 0x7FFFFFFF) % 10 ** digits
return str(code).zfill(digits)
print(totp('JBSWY3DPEHPK3PXP'))
No network calls, no hidden server, just a secret, a clock and a hash. On Linux you can get the same code from the command line:
# Linux: get the current code in a terminal (oathtool package)
oathtool --totp -b JBSWY3DPEHPK3PXP
# Check that server time is synchronised
timedatectl status
Why accurate time matters
If the phone or server clock drifts by a minute or two, codes stop matching. Servers usually accept one step either side, roughly 30 seconds of drift. Keep automatic time enabled on the phone and NTP running on the server, and check the clock and time zone first when codes are rejected.
What TOTP does and does not stop
Codes are generated on the device, so SIM swapping and SMS interception do not affect them, and they work offline. TOTP does not stop phishing, though: a fake site can relay your password and current code within the 30-second window. Only domain-bound passkeys defeat that.
Setup and backup codes
Save the backup codes the site gives you, printed or in a password manager; they may be your only way back in if the phone is lost. Google Authenticator can now sync codes via your Google account, Microsoft Authenticator has its own backup, and the open-source Aegis on Android supports encrypted export. Move accounts before wiping an old phone and test each one.
Example: a web studio in Tashkent
A small studio in Yakkasaray maintains WordPress sites for over ten clients, each with admin, domain, hosting, email and repository accounts. After a developer's work email password showed up in a breach, the studio made TOTP mandatory everywhere, added a 2FA plugin to the WordPress sites and kept backup codes in a restricted vault. When someone leaves, 2FA is re-enrolled with new secrets. For domain and hosting accounts, check which verification options your sayt.uz cabinet offers in profile settings or via support.
Adding TOTP to your own site
Generate a random secret per user, show the otpauth link as a QR code and confirm enrollment with the first code. Encrypt secrets at rest, reject a code reused within the same step and limit attempts. With pyotp:
import pyotp
secret = pyotp.random_base32() # created once per user
totp = pyotp.TOTP(secret)
uri = totp.provisioning_uri(name='ali@misol.uz', issuer_name='Misol')
# render uri as a QR code for the user
totp.verify(kiritilgan_kod, valid_window=1) # allow +-30 seconds of drift
Hash backup codes like passwords, and require a current code or password before disabling 2FA.