Sub topic 7
Securing the account with two-factor authentication
This chapter assumes you have registered an exchange account, as covered in the previous chapter, and that you can install an app on your phone. You do not need to know cryptography, but you should be comfortable reading a short Python script.
Why a password is not enough
A password is something you know. If it leaks in a data breach, is reused on another site, or is typed into a fake login page, an attacker can sign in as you. On an exchange the damage is direct: funds can be withdrawn to an address the attacker controls, and blockchain transfers cannot be reversed.
Two-factor authentication (2FA) adds a second proof from a different category, usually something you have, such as your phone or a hardware key. The attacker then needs both factors, not just the password.
Common 2FA methods compared
| Method | How it works | Main weakness |
|---|---|---|
| SMS code | A code is texted to your phone number | SIM swapping, where an attacker convinces your carrier to move your number to their SIM. Texts can also be intercepted |
| Email code | A code is sent to your inbox | If the email account is compromised, both factors fall together |
| Authenticator app (TOTP) | An app generates a code from a shared secret and the clock | The code can be phished in real time. Losing the phone without a backup locks you out |
| Security key (FIDO2/WebAuthn) | A hardware device signs a challenge tied to the site's domain | Costs money. You must register a spare key |
Ranked by resistance to attack, security keys come first, authenticator apps second, and SMS last. Many exchanges let you enable several methods. Use the strongest one the exchange offers, and keep SMS only if it is the sole option.
How an authenticator app works
An authenticator app implements TOTP, the time-based one-time password algorithm from RFC 6238. During setup the exchange shows a QR code. That code contains a shared secret, a random string encoded in Base32. Your app stores the secret. The exchange stores the same secret.
Every 30 seconds both sides compute the same six digits:
- Divide the current Unix time by 30 to get a counter.
- Compute an HMAC-SHA1 of the counter, using the secret as the key.
- Take four bytes from the result at an offset given by its last nibble, a step called dynamic truncation.
- Reduce that number modulo 10^6 to get six digits.
No network connection is needed on your phone. The only requirement is that the two clocks roughly agree.
import base64
import hashlib
import hmac
import struct
import time
def totp(secret_b32, step=30, digits=6, at=None):
secret_b32 = secret_b32.replace(" ", "").upper()
secret_b32 += "=" * (-len(secret_b32) % 8)
key = base64.b32decode(secret_b32)
counter = int((time.time() if at is None else at) // step)
digest = hmac.new(key, struct.pack(">Q", counter), hashlib.sha1).digest()
offset = digest[-1] & 0x0F
value = struct.unpack(">I", digest[offset:offset + 4])[0] & 0x7FFFFFFF
return str(value % 10 ** digits).zfill(digits)
# RFC 6238 test secret (ASCII "12345678901234567890") at Unix time 59
print(totp("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", at=59)) # 287082The at argument lets you fix the time so the output is reproducible. The RFC publishes an 8-digit value of 94287082 for this input, and the last six digits are 287082. Never run this on a real exchange secret in a shared environment. Anyone who sees the secret can generate your codes forever.
Setting it up step by step
- Sign in on a trusted device and open the security settings.
- Choose the authenticator app option and scan the QR code with an app on your phone.
- Before you confirm, copy the text version of the secret, often labelled the backup key or setup key. Write it on paper. Store it away from the phone.
- Enter the six-digit code to confirm. The exchange will only switch 2FA on after a valid code.
- Test it: sign out and sign back in.
- Turn on 2FA for withdrawals and for changes to security settings, if these are separate toggles.
Settings that protect your funds
- Anti-phishing code. Many exchanges let you set a word or phrase that appears in every genuine email from them. A fake email will lack it.
- Withdrawal address allowlist. Restrict withdrawals to addresses you have pre-approved. New addresses often face a waiting period such as 24 hours, which gives you time to react if someone else adds one.
- Withdrawal lock after changes. Some platforms freeze withdrawals for a day or two after a password or 2FA change. Leave this on.
- Device and session review. Check the list of logged-in devices and sign out ones you do not recognize.
- A separate email. Use an email address for the exchange that you use nowhere else, and protect it with its own 2FA.
Common errors
- "Invalid code" on every attempt. The phone clock has drifted. TOTP depends on time. Enable automatic time in the phone settings, or use your authenticator's time-correction option, and try again.
- Typing a code just as it expires. Wait for the next code. The server may accept one adjacent window, but not more.
- New phone, no backup. Without the backup key or the app's own export, you must go through the exchange's 2FA reset. That usually involves a new selfie and ID check and can take days.
- Moving only the app, not the secret. Some authenticator apps do not sync. Check that the app supports transfer or encrypted backup before you rely on it.
- Relying on SMS only. If a SIM swap happens, the attacker receives your codes. Switch to an app or key.
- Entering a code on a lookalike site. TOTP codes are not tied to a domain, so a fake page can relay them to the real site. Only security keys block this. Type the exchange address yourself or use a bookmark.
Choosing a setup
For most users, an authenticator app plus the allowlist and anti-phishing code is a good balance. If you hold a large balance, add a hardware security key and register two, keeping the spare somewhere separate. Decide your recovery plan before you need it.
Exercise
You set up an authenticator app, and a week later your exchange says "Invalid code" every time, though the app shows a fresh code. Your password works. Name the most likely cause, how to fix it, and why a text-message code would not have this problem but is still the weaker choice.
Show answer
The most likely cause is clock drift on the phone. TOTP derives the code from the current time in 30-second steps, so if the phone clock is off by more than about a step, the exchange computes different digits. Turn on automatic date and time (or use the app's time-correction feature) and retry. An SMS code is generated by the exchange and sent to you, so it does not depend on your phone clock. It is still weaker because an attacker can take over your number through a SIM swap or intercept the message, and then receive your codes.