Story Details

  • Behind the 6-digit code: Building HOTP and TOTP from scratch

    Posted: 2025-04-11 13:06:17

    This blog post explains how one-time passwords (OTPs), specifically HOTP and TOTP, work. It breaks down the process of generating these codes, starting with a shared secret key and a counter (HOTP) or timestamp (TOTP). This input is then used with the HMAC-SHA1 algorithm to create a hash. The post details how a specific portion of the hash is extracted and truncated to produce the final 6-digit OTP. It clarifies the difference between HOTP, which uses a counter and requires manual synchronization if skipped, and TOTP, which uses time and allows for a small window of desynchronization. The post also briefly discusses the security benefits of OTPs and why they are effective against certain types of attacks.

    Summary of Comments ( 56 )
    https://news.ycombinator.com/item?id=43653322

    HN users generally praised the article for its clear explanation of HOTP and TOTP, breaking down complex concepts into understandable parts. Several appreciated the focus on building the algorithms from the ground up, rather than just using libraries. Some pointed out potential security risks, such as replay attacks and the importance of secure time synchronization. One commenter suggested exploring WebAuthn as a more secure alternative, while another offered a link to a Python implementation of the algorithms. A few discussed the practicality of different hashing algorithms and the history of OTP generation methods. Several users also appreciated the interactive code examples and the overall clean presentation of the article.