Predict the output
What code does the RFC 6238 test vector give at T = 59?
import hashlib
import hmac
import struct
def totp(secret: bytes, unix_time: int, digits: int = 8) -> str:
counter = struct.pack(">Q", unix_time // 30)
mac = hmac.new(secret, counter, hashlib.sha1).digest()
offset = mac[-1] & 0x0F
code = struct.unpack(">I", mac[offset:offset + 4])[0] & 0x7FFFFFFF
return str(code % 10**digits).zfill(digits)
# RFC 6238 test vector: SHA-1 secret, T = 59 s
print(totp(b"12345678901234567890", 59))Hint 1 of 1
Which time step is T = 59?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 94287082
Correct. That's the published SHA-1 vector for T = 59 s.
TOTP hashes the 30-second time step with the shared secret and truncates the HMAC to digits. At T = 59 the step is 1, and RFC 6238 lists 94287082 for the 8-digit SHA-1 case, so the code is deterministic.
Why not the others?
- B
287082. That's the 6-digit form; this call asks for 8 digits. - C
00000059. The code is an HMAC, not the time. - D A different code each run. Same secret and time step always give the same code.