otp

A Go library for generating and validating TOTP and HOTP one-time passcodes, compatible with Google Authenticator.

Library
Go
vv1.5.0
2,964 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
43 /100 Fair
Development Activity 0
Maintenance 20
Community 52
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
66 /100 Good
Architecture 78
Code Quality 78
Innovation 42
Learning Curve 65

otp is a Go library implementing both Time-based One-Time Passwords (TOTP, RFC 6238) and HMAC-based One-Time Passwords (HOTP, RFC 4226), the same algorithms used by Google Authenticator, GitHub, and most two-factor authentication apps. It handles key generation, QR code image creation for enrollment, and constant-time passcode validation, so applications can add TOTP/HOTP-based two-factor authentication without hand-rolling the cryptography.

The library exposes simple Generate/Validate/GenerateCode functions for the common Google-Authenticator-compatible case, along with *Custom variants that expose every RFC parameter (period, skew, digit count, hash algorithm, and even a Steam Guard-style encoder) for applications with non-standard requirements.

What You Get

  • TOTP generation and validation (RFC 6238) with configurable period, skew, digit count, and hash algorithm
  • HOTP generation and validation (RFC 4226) as the counter-based building block TOTP is layered on
  • QR code image generation via Key.Image() for scannable enrollment in authenticator apps
  • Support for SHA1, SHA256, SHA512, and MD5 HMAC algorithms, plus 6- and 8-digit codes
  • A Steam Guard-compatible passcode encoder alongside the standard numeric encoder

Common Use Cases

  • Adding two-factor authentication (2FA) login flows to a web or API backend
  • Generating enrollment QR codes for users to scan with Google Authenticator, Authy, or similar apps
  • Validating time-based passcodes server-side during login or sensitive-action confirmation
  • Implementing custom OTP schemes (non-standard periods, digit counts, or algorithms) for internal tooling

Under The Hood

Architecture

The library is organized as flat, single-responsibility packages: the root otp package holds shared types (Key, Algorithm, Digits, Encoder) and the Key URL-parsing/generation logic in otp.go; the hotp subpackage implements the RFC 4226 primitive (GenerateCodeCustom, ValidateCustom) that both TOTP and HOTP callers rely on; the totp subpackage layers time-windowing (period/skew-based counter derivation) on top of hotp.GenerateCodeCustom/ValidateCustom, delegating all HMAC and truncation work downward rather than duplicating it — a clean two-layer composition where TOTP is effectively HOTP with a counter derived from time. A small internal package centralizes URL query encoding so both totp.Generate and hotp.Generate produce identical otpauth:// URI formatting, and a separate interop module (its own go.mod) isolates cross-implementation test vectors from the core library’s dependency graph. There is no runtime state and no dependency injection — all functions are pure given secret/counter/time inputs — so the one thing that would ripple everywhere is a change to Key’s URL-based internal representation, since both totp and hotp construct Key values through otp.NewKeyFromURL.

Tech Stack

go.mod declares the module at Go 1.12 with only two direct dependencies: a barcode-encoding library used for QR code image rendering, and a test-assertion library used only in tests. The cryptographic core relies entirely on the Go standard library — HMAC, SHA1/SHA256/SHA512, MD5, constant-time comparison, secure random generation, base32 encoding, and URL handling for the otpauth:// scheme. There is no web framework, ORM, or database, which fits a pure algorithm library with no runtime dependencies beyond the standard library and one small image-encoding package. CI runs the test suite across multiple architectures. There is no build tooling beyond the Go toolchain and no deployment target, since the project is consumed as an imported package rather than run as a service.

Code Quality

Test coverage exists at every layer — the root package, the totp subpackage, and the hotp subpackage each carry a dedicated test file, plus a separate interop test that cross-checks output against another OTP implementation’s test vectors, all using a test-assertion library for readable comparisons. Error handling is explicit and typed via sentinel errors rather than generic errors or panics, and passcode comparison uses a constant-time comparison specifically to avoid timing side-channel leaks — a security-conscious choice not every comparable library makes. Naming is idiomatic Go with exported doc comments throughout. There is no modern linter configuration present, and CI appears limited to running the test suite rather than static analysis, so quality enforcement here is test-driven rather than lint-driven.

What Makes It Unique

otp does not invent a new protocol — TOTP and HOTP are IETF standards, and the library’s value is a clean, dependency-light, spec-compliant Go implementation rather than a novel algorithmic contribution. Its most distinctive design choice is representing a Key as a parsed otpauth:// URL rather than a bespoke struct, so key generation, parsing, and serialization all round-trip through the exact URI format that Google Authenticator and compatible apps consume, making interop with third-party parsers close to free. It also supports an uncommon Steam Guard-style passcode alphabet alongside the standard numeric encoder, a niche feature most comparable OTP libraries omit entirely.

Used by 18 apps in this directory

Go
69%
Other

Hanko

Authentication · Security

9,045

Open source, self-hostable authentication platform with passkeys, SAML SSO, and OAuth — the privacy-first alternative to Auth0 and Clerk.

View details
90
Repo Health
81
Technical
66
Dependency
Built with
Go 69%
TypeScript 27%
Updated 2 days ago
Go
51%
AGPL 3.0

listmonk

Blogging · Marketing

23,944

High-performance, self-hosted newsletter and mailing list manager packaged as a single binary with built-in analytics, transactional messaging, and multi-channel delivery.

View details
90
Repo Health
74
Technical
64
Dependency
Built with
Go 51%
JavaScript 22%
HTML 20%
Updated yesterday
Go
97%
Other

Netmaker

Automation · Security

11,836

Automate secure WireGuard mesh networks from homelab to enterprise scale without manual configuration.

View details
92
Repo Health
79
Technical
70
Dependency
Built with
Go 97%
Updated 2 days ago
Go
67%
MIT

nginx ignition

Developer Tools · Devops

2,141

A modern web UI for nginx that eliminates config file editing with visual virtual hosts, SSL automation, Docker integrations, and real-time traffic analytics.

View details
74
Repo Health
80
Technical
67
Dependency
Built with
Go 67%
TypeScript 28%
Updated 2 weeks ago
Go
91%
Apache 2.0

Ory Kratos

Authentication

13,933

API-first identity and user management that handles login, registration, MFA, and recovery so your application never has to.

View details
69
Repo Health
78
Technical
65
Dependency
Built with
Go 91%
Updated 2 months ago
Go
54%
GPL 3.0

SafeLine

Security

22,923

Self-hosted WAF with a custom semantic detection engine and ML-powered threat analysis that blocks web attacks without cloud dependency.

View details
87
Repo Health
72
Technical
69
Dependency
Built with
Go 54%
C++ 19%
Updated 3 days ago
Go
75%
AGPL 3.0

Teleport

Authentication · Security

21,016

Zero-trust infrastructure access platform that replaces credentials and VPNs with short-lived certificates, SSO, and identity-aware proxies for SSH, Kubernetes, databases, RDP, and AI agents.

View details
94
Repo Health
81
Technical
68
Dependency
Built with
Go 75%
TypeScript 16%
Updated 5 days ago
Go
66%
AGPL 3.0

Vikunja

Project Management

5,833

Self-hosted task management with natural-language quick-add, multiple views, and a fully documented REST API — your tasks, your infrastructure, zero lock-in.

View details
86
Repo Health
81
Technical
65
Dependency
Built with
Go 66%
TypeScript 24%
Updated yesterday
Go
74%
AGPL 3.0

ZITADEL

Authentication

15,418

Open-source, API-first identity platform delivering multi-tenancy, Passkeys, OIDC, SAML, and SCIM without vendor lock-in.

View details
92
Repo Health
81
Technical
65
Dependency
Built with
Go 74%
TypeScript 12%
Updated 2 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers