When an authenticator code is rejected, the fault sits in one of four steps: decoding the secret, turning the clock into a counter, the HMAC, or the truncation to six digits. Writing those four steps yourself is the quickest way to know which one to look at. Here they are in Node with no packages, each with a known answer to check against.
The known answer is the test vector from RFC 6238. Its secret is the ASCII text 12345678901234567890, which an authenticator app takes in Base32 as GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ. At 59 seconds after 1 January 1970 the 8-digit SHA-1 code is 94287082, and the 6-digit code is its last six digits, 287082. A function that prints those two is right.
Step 1: the Base32 secret becomes bytes
The setup key printed under a QR code is Base32: the letters A to Z and the digits 2 to 7, each character carrying 5 bits. Spaces, padding and lower case do not matter, so strip them first.
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; function base32Decode(input) { const clean = input.toUpperCase().replace(/[\s=]/g, ''); let bits = 0, value = 0; const out = []; for (const ch of clean) { const idx = ALPHABET.indexOf(ch); if (idx === -1) throw new Error(`not Base32: ${ch}`); value = (value << 5) | idx; bits += 5; if (bits >= 8) { out.push((value >>> (bits - 8)) & 0xff); bits -= 8; } } return Buffer.from(out); } Check: the Base32 decoder, set to Decode and Base32, turns the test secret into 12345678901234567890 and reports 20 bytes. That size is normal: most services use 20 random bytes, 160 bits, which is 32 Base32 characters.
Step 2: the clock becomes a counter
Unix time in seconds, divided by the period and rounded down. The period is 30 seconds unless the service says otherwise. The counter is then written as 8 bytes, big-endian.
const counter = Math.floor(time / period); // 59 / 30 -> 1 const msg = Buffer.alloc(8); msg.writeBigUInt64BE(BigInt(counter)); // 00 00 00 00 00 00 00 01 For a real date: at 12:00:10 UTC on 8 November 2026 the counter is 59804640. That step runs from 12:00:00 to 12:00:30, so the code on screen then has 20 seconds left.
Step 3: HMAC-SHA1 of those 8 bytes
const mac = crypto.createHmac('sha1', base32Decode(secret)).update(msg).digest(); For the test key and counter 1 this is 20 bytes:
75a48a19d4cbe100644e8ac1397eea747a2d33ab You can check this step on its own. The counter bytes are not printable, so a text box cannot hold them, but a file can:
printf '\x00\x00\x00\x00\x00\x00\x00\x01' > counter.bin openssl dgst -sha1 -hmac 12345678901234567890 counter.bin The HMAC generator with SHA-1, the key typed as Text and the message switched to File gives the same hex for counter.bin. If your own value differs here, look at how the counter was encoded: the text "1" and the 8-byte number 1 are different messages.
Step 4: truncate to digits
The low 4 bits of the last byte pick an offset. The 4 bytes starting there, with the top bit cleared, make a number, and the code is its last 6 or 8 digits.
const offset = mac[mac.length - 1] & 0x0f; // 0xab & 0x0f = 11 const bin = mac.readUInt32BE(offset) & 0x7fffffff; // 0xc1397eea -> 0x41397eea return String(bin % 10 ** digits).padStart(digits, '0'); 0x41397eea is 1094287082. The last 8 digits are 94287082 and the last 6 are 287082. Keep the padStart: a code whose number begins with zeros still has to be six characters long.
Steps 2 to 4 as one function
const crypto = require('node:crypto'); function totp(secret, { time = Date.now() / 1000, period = 30, digits = 6, algo = 'sha1' } = {}) { const counter = Math.floor(time / period); const msg = Buffer.alloc(8); msg.writeBigUInt64BE(BigInt(counter)); const mac = crypto.createHmac(algo, base32Decode(secret)).update(msg).digest(); const offset = mac[mac.length - 1] & 0x0f; const bin = mac.readUInt32BE(offset) & 0x7fffffff; return String(bin % 10 ** digits).padStart(digits, '0'); } const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; console.log(totp(secret, { time: 59, digits: 8 })); // 94287082 console.log(totp(secret, { time: 59 })); // 287082 Both lines print the RFC values on Node 24. For a live check I pasted the same secret into a TOTP generator this afternoon. At 14:09 IST it showed step 59856079 with 713 395 as the previous code, 827 335 as the current one and 749 271 as the next; totp() for steps 59856078, 59856079 and 59856080 printed the same three.
Checking a code someone typed
Servers usually accept a code from one step before or after the current one, so a code typed a few seconds late still works. Returning which step matched also tells you roughly how far off the user's clock is.
function verify(secret, code, { window = 1, period = 30, ...opts } = {}) { const now = Date.now() / 1000; for (let step = -window; step <= window; step++) { const expected = totp(secret, { ...opts, period, time: now + step * period }); if (expected.length === code.length && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(code))) { return step; // 0 on time, -1 one step late, +1 one step early } } return null; } timingSafeEqual is there for a reason: an ordinary string comparison stops at the first different character, so its timing leaks how much of a guess was right. A constant-time comparison checks every byte every time.
When the phone and the server still disagree
- The clock. A code that only works one step early or late means a clock about 30 seconds off.
- Settings the app ignores. Google Authenticator historically used SHA-1, six digits and 30 seconds whatever the otpauth link asked for. Keep those defaults unless you control both sides.
- The secret. One wrong character in the Base32 key, or a different algorithm, digit count or period on the server, changes every code.
The same example is worked through by hand in the TOTP guide. I wrote this with AI help; the test values come from the linked pages as they read on 26 November 2026, and every output above is from running the code.