Top
Best
New

Posted by dogacel 4/11/2025

Behind the 6-digit code: Building HOTP and TOTP from scratch(blog.dogac.dev)
250 points | 74 commentspage 2
thatkid02 4/15/2025|
And I put it like this https://medium.com/@thatkid02/javascript-2-factor-authentica...
coppsilgold 4/15/2025||
It's often a good idea to set up TOTP on accounts just because they may treat you differently due to having 2FA enabled. It would be harder to lose a gmail account to their "security" systems if you add TOTP to it for example. In the case of gmail adding it is a hassle involving devtools to emulate a hardware key first then add TOTP and then delete the hardware 2FA.

Some password managers such as KeepassXC have TOTP incorporated into them and you can have it available right next to the password. It may defeat the purpose of 2FA under some assumptions.

dogacel 4/15/2025|
I personally use 1Password with hardware keys where possible.

> It may defeat the purpose of 2FA

True, I think this as a mid-step of smooth transition from plain-text passwords to secure keys. You kinda get the benefit of both.

Also those apps are secured much better than a traditional password manager as browser auto-fill for example.

coppsilgold 4/15/2025||
> I think this as a mid-step of smooth transition from plain-text passwords to secure keys.

This is not what I meant. Storing the TOTP next to the password means you don't really have 2FA as it's a single point of failure. Still better than nothing especially when the objective is what I stated in the first paragraph.

jqpabc123 4/16/2025||
I implemented TOTP as a command line app doing lookup and generation by pulling secrets from a locally encrypted password file.

And before someone asks, the decrypt key is only stored in my head and the app fails silently after a significant delay if the decrypt fails.

What I don't get is how HOTP is anything but a fail waiting to happen if used across an unreliable network. Maybe this explains why I have yet to encounter a real world deployment of HOTP.

GoblinSlayer 4/16/2025||
In my experience HOTP works fine, why not. The real world deployment is a replacement for sms otp.
dogacel 4/16/2025||
Client and server can potentially agree on the counter with a handshake.
jqpabc123 4/17/2025||
How does such a handshake agreement get triggered?

How does the server know that the handshake request is not malicious? 2FA that is resettable on demand (without 2FA) effectively voids the whole concept does it not?

With HOTP, the counter is the shared secret --- but a dynamic and potentially unstable one. One failed request or one missed response and the counters on client and server are no longer in sync. Hence, a failure waiting to happen on an unreliable network.

dogacel 4/18/2025||
Why counter is the shared secret? In TOTP time is the counter and it is obviously not secret, so there is no reason to think the counter would be secret as well. Clients can sync their counter to match the server.
jqpabc123 4/18/2025||
In TOTP, time exists independent of both client and server and as you point out, is no secret to either.

In HOTP, the secret counter is not independent and must remain synchronized between client and server.

A counter that can be synchronized on demand is kinda superfluous --- not really secret and not terribly relevant either. All else being equal, an attacker can sync up just as easily as a legitimate client so why bother with the counter?

I expect HOTP exists somewhere out there in the real world but I have yet to encounter it. Every 2FA I have actual experience with has been TOTP.

dogacel 4/18/2025||
> A counter that can be synchronized on demand is kinda superfluous --- not really secret and not terribly relevant either. All else being equal, an attacker can sync up just as easily as a legitimate client so why bother with the counter?

A unique counter for each authorization attempt ensures the resulting key is different for each attempt, which makes replay attacks not possible. I agree if you sync the counter two ways, it is better to use a "nonce", a totally random secret each time.

ur-whale 4/15/2025||
https://github.com/pcarrier/gauth
rothfuss 4/15/2025||
Thanks for the read, I learnt something about HOTP/TOTP today.

I would like to know why the clocks are all weird though - the numbers aren't in the right places. Were the images in this blog post "AI" generated?

dogacel 4/15/2025|
Nope not AI generated, I have used excalidraw. Only the cover page is AI generated.

Clock drawing was an asset, I didn't really spent time trying to match the time on clock to the time mentioned by the actors.

coolThingsFirst 4/15/2025||
> Like the traditional password authentication approach, the user and the authority (server) still needs to agree on a common secret key.

Not sure what you mean by this, the server checks the hashed version of the password.

dogacel 4/15/2025||
Hashing is done before storing the secret on the server side. Therefore they still need to communicate regarding the intial secret.
rgthyjuk 4/17/2025||
[dead]
sksxihve 4/15/2025||
On a side note, does anyone know why banks still rely on sms 2fa codes instead of TOTP? Is there some regulatory issue that makes it more difficult?
UncleMeat 4/15/2025||
Everybody with a phone has SMS baked in. SMS also has a recovery process if you drop your phone in the toilet. Ultimately, this improved user experience outweighs the security benefit to TOTP for many organizations.

TOTP also doesn't stop the biggest threat that SMS faces: phishing. Saving you from sim-swap attacks is just not a particular huge increase in security posture.

My bank at least offers TOTP as an option, but the huge majority of people are going to enroll with SMS.

Rygian 4/15/2025|||
My two banks require additional approval via push notification to the phone app. No SMS involved.

(In France.)

dogacel 4/15/2025||
Some banks in Switzerland give customers a device that generates TOTP codes.
ajsnigrutin 4/15/2025|
What is it with modern web design... can't even read a third of the page, and they already want my email to subscribe...
dogacel 4/15/2025|
Clicking anywhere else discards it.

I have removed the popup anyway, seems like most people don't like it.

More comments...