otp

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

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

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
43/100Fair
Development Activity0
Maintenance20
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
66/100Good
Architecture78
Code Quality78
Innovation42
Learning Curve65

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 17 apps in this directory

Go
55%
Apache 2.0

Authgear

Authentication

2,014

Open-source, self-hostable authentication platform with passkeys, biometric login, SSO, MFA, and GraphQL admin API — a full Auth0/Clerk/Firebase alternative for SaaS and mobile apps.

View details
88
Repo Health
81
Technical
69
Dependency
Built with
Go55%
HTML25%
TypeScript17%
Updated 4 days ago
Go
52%
MIT

Bytebase

Devops

14,449

An open-source database CI/CD and DevSecOps platform — schema migration review, GitOps-driven changes, data masking, and access control across MySQL, PostgreSQL, Oracle, Snowflake, MongoDB, and more.

View details
91
Repo Health
73
Technical
69
Dependency
Built with
Go52%
TypeScript39%
Updated yesterday
Go
49%
Other

Cosmos-Server

Security · Authentication

6,134

All-in-one self-hosted home server with SmartShield anti-DDoS, Nebula mesh VPN, automatic HTTPS, and a 250-app marketplace — all secured behind a unified auth layer.

View details
86
Repo Health
59
Technical
65
Dependency
Built with
Go49%
JavaScript48%
Updated yesterday
Go
92%
AGPL 3.0

Cozy Stack

File Storage · Productivity

1,270

Self-hosted personal cloud platform that unifies your files, apps, and devices in one private space you fully control.

View details
94
Repo Health
77
Technical
67
Dependency
Built with
Go92%
Updated 2 days ago
Go
38%
MIT

ezBookkeeping

Invoicing Finance

5,494

Lightweight self-hosted personal finance manager with AI receipt scanning, multi-currency support, and MCP integration for complete data privacy.

View details
88
Repo Health
80
Technical
74
Dependency
Built with
Go38%
Vue33%
TypeScript25%
Updated yesterday
Go
51%
AGPL 3.0

Filestash

File Storage

14,560

A self-hosted file management platform that unifies access to S3, SFTP, SMB, FTP, WebDAV, NFS, Git, SharePoint, and 20+ other storage backends through a single extensible web interface.

View details
79
Repo Health
67
Technical
66
Dependency
Built with
Go51%
JavaScript35%
Updated 3 days ago
Go
83%
MIT

Gitea

Devops · Developer Tools · Project Management

57,677

Self-hosted DevOps in a single Go binary — Git hosting, GitHub Actions-compatible CI/CD, and 30+ package registries without any SaaS dependency.

View details
93
Repo Health
79
Technical
66
Dependency
Built with
Go83%
Updated today
Go
68%
MIT

Gogs

Developer Tools

47,783

The painless self-hosted Git service that runs on anything from a Raspberry Pi to a $5 cloud droplet, delivering GitHub-like workflows as a single Go binary.

View details
96
Repo Health
79
Technical
72
Dependency
Built with
Go68%
Go Template16%
TypeScript10%
Updated 4 days ago
Go
65%
Other

Hanko

Security · Authentication

9,016

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

View details
89
Repo Health
81
Technical
69
Dependency
Built with
Go65%
TypeScript30%
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

Search