Fork me on GitHub

Step-by-Step TOTP

Inputs

K (shared secret)
Digit (number of digits) WARNING: At least 6 digits are required, according to RFC 4226.
C (counter value)
C (counter value)
T0 (time to start counting)
X (time step in seconds)
current time
T (number of time steps) 0

TOTP Code

000000

Intermediate values

K (Base32-decoded)
C (converted to bytes)
HS = HMAC-SHA-1(K,C)
Details
key
(use H(K) because the input K is longer than 64 bytes)
K XOR ipad
K XOR opad
K XOR ipad, text
H(K XOR ipad, text)
K XOR opad, H(K XOR ipad, text)
H(K XOR opad, H(K XOR ipad, text))
Sbits = DT(HS)
Details
OffsetBits = low-order 4 bits of HS[19]
Offset = StToNum(OffsetBits)
P = HS[OffSet]...HS[OffSet+3]
Last 31 bits of P
Snum = StToNum(Sbits)
D = Snum mod 10^Digit

References