Pair two devices on a LAN with a short human-typed code and an authenticated key exchange. Package codepair, module github.com/Totative/go-codepair.
Two devices that share a network but no trust can be paired by a person reading a short code from one and typing it into the other. This package is the cryptographic core of that exchange: it mints the code and runs CPace, a password-authenticated key exchange, so the two devices agree on a key only if they hold the same code, and an attacker on the wire gets one guess per run.
go get github.com/Totative/go-codepairA worked example and the contracts: HUMANS.md.
- Codes:
NewCodedraws each character uniformly from a caller-chosen alphabet, andNormalizefolds a typed code into the form both sides use, so case and separators never matter.Validateenforces a 32-bit entropy floor.NewJoinerandNewHostrefuse a code that is not exactlyCodeLengthcharacters fromAlphabet(ErrCode). - Key exchange:
JoinerandHostrun CPace (draft-irtf-cfrg-cpace-21, ristretto255, SHA-512), bind the protocol label and both ids and display names, and confirm the key in both directions before they return aPairing. - Peer key: the long-term key is HKDF-SHA-512 of the exchange's session key, so it never depends on a guessable value.
- Rate limiting:
NewLimiterreturns an error unless its bounds are positive.Host.Acceptcharges aLimiteronce per run, locks a remote out after a set number of failures, and reports when failures in all have reached the point where the caller should drop the code. Remotes are identified by IP only (global IPv6 by /64, link-local IPv6 by interface), so users who share a NAT or a link share that limit.
| Doc | Audience |
|---|---|
| HUMANS.md | Running and using the package |
| AGENTS.md | Internals, invariants, and why |
| CONTRIBUTING.md | Process: commits, tests, docs |
| SECURITY.md | Vulnerability disclosure |
| CHANGELOG.md | Release notes |
Apache-2.0, see LICENSE. © 2026 Damon Blais.