A 6-digit email code is only a million possibilities, so the code itself isn't what protects a shared file: the rules around it are. Store it as a keyed digest (HMAC-SHA256 with a server secret), never as the code or a plain hash: in my test, a plain SHA-256 of a 6-digit code was reversed by trying every value in 62 milliseconds. Bind the digest to the share so a code for one file can't open another. Expire it after 10 minutes, allow 5 attempts, counted before comparing, invalidate the old code when a new one is sent, and limit resends with a 60-second cooldown. Finally, consume the code and create the session in one atomic step: in the test, 100 concurrent submissions of the correct code created exactly one session. With those limits, an attacker gets at most about 300 guesses an hour, a 0.03% chance of hitting the right code.
Each rule closes one attack; the numbers on the right come from the tests below.
The rules come from the guest-sharing design of PlantDocket, a factory document control system I'm working on (spec): an employee shares one approved drawing with a contractor's email address, and the contractor proves they own that inbox before downloading.
How does the whole flow work?
Six steps; the code only proves inbox access, and every download is authorised again.
Why not a plain link?
A link alone is a bearer token: anyone it's forwarded to, or who finds it in a proxy log, can download the file. Requiring a code sent to the invited address means a forwarded link is useless without access to that inbox. The link still matters: keep its token high-entropy, store only its digest, and redirect to a clean URL after the first visit so the token doesn't sit in browser history or referrer headers.
The store
```go title="otp.go: issue"
func (s *Store) digest(grant, code string) []byte {
m := hmac.New(sha256.New, s.key)
fmt.Fprintf(m, "%s:%s", grant, code) // bound to the grant: a code for one share is useless for another
return m.Sum(nil)
}
// Issue creates a new 6-digit code for the grant, replacing (and invalidating) any earlier one.
func (s *Store) Issue(grant string) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
now := s.Now()
if c := s.active[grant]; c != nil && now.Sub(c.createdAt) < Cooldown {
return "", ErrCooldown
}
n, err := rand.Int(rand.Reader, big.NewInt(1_000_000))
if err != nil {
return "", err
}
code := fmt.Sprintf("%06d", n.Int64())
s.active[grant] = &challenge{digest: s.digest(grant, code), expires: now.Add(TTL), createdAt: now}
return code, nil // send by email; never log or store it
}
`crypto/rand` with `rand.Int` gives a uniform code; `math/rand` or a modulo of random bytes would not. `%06d` keeps leading zeros, so `004213` stays six digits. ```go title="otp.go: verify" func (s *Store) Verify(grant, code string) error { s.mu.Lock() defer s.mu.Unlock() c := s.active[grant] if c == nil || c.consumed || s.Now().After(c.expires) || c.attempts >= MaxAttempts { return ErrInvalid } c.attempts++ // count before comparing, so a burst can't exceed the limit if subtle.ConstantTimeCompare(c.digest, s.digest(grant, code)) != 1 { return ErrInvalid } c.consumed = true s.sessions++ return nil } Every failure returns the same ErrInvalid: expired, used, wrong and over-limit all look identical, so the response doesn't tell an attacker which case they hit.
Why a keyed digest?
If the codes table leaks (a backup, a log, a SQL injection elsewhere), a plain hash gives the codes away:
```go title="brute_test.go"
leaked := sha256.Sum256([]byte("g:482913")) // an unkeyed digest from a leaked table
for i := 0; i < 1_000_000; i++ {
c := fmt.Sprintf("%06d", i)
if sha256.Sum256([]byte("g:"+c)) == leaked {
found = c
break
}
}
// recovered 482913 from a plain SHA-256 in 62ms
Three runs, 62 ms each, on a laptop. A slow password hash like bcrypt raises the cost of each guess, but a million candidates is still a small search. An **HMAC with a secret key** that isn't stored with the data does help: without the key, the digests can't be checked offline at all. ## What the tests show Seven tests pass under `go test -race` (the brute force above is one of them): * **Happy path and reuse:** the right code works once; the second use fails, and the same code fails against a different grant. * **Expiry:** at 10 minutes and 1 second, the right code is refused. * **Attempt limit:** after 5 wrong guesses, even the **correct** code fails. Counting before the comparison is what makes this hold under concurrent requests. * **Resend:** a second request within 60 seconds is refused; after it, the new code works and the old one doesn't. * **Race:** 100 goroutines submit the correct code at once; **exactly 1** creates a session. * **Odds:** 60 codes an hour × 5 attempts = 300 guesses against 1,000,000 codes, **0.03%** per hour. Add a per-IP limit and an hourly cap per share to keep it there. ## In production * **Atomic consumption in the database.** The attempt counter, the comparison, the consumption and the session belong in one transaction. I ran the race against SQLite (pure-Go `modernc.org/sqlite`, WAL mode): ```go title="db.go: redeem" func Redeem(db *sql.DB, key []byte, grant, code string, now time.Time) error { tx, err := db.Begin() if err != nil { return err } defer tx.Rollback() var digest []byte err = tx.QueryRow(`UPDATE challenges SET attempts = attempts + 1 WHERE grant_id = ? AND consumed = 0 AND attempts < 5 AND expires_at > ? RETURNING digest`, grant, now.Unix()).Scan(&digest) if errors.Is(err, sql.ErrNoRows) { return ErrInvalid } else if err != nil { return err } if subtle.ConstantTimeCompare(digest, Digest(key, grant, code)) != 1 { return tx.Commit() // keep the attempt count } res, err := tx.Exec(`UPDATE challenges SET consumed = 1 WHERE grant_id = ? AND consumed = 0`, grant) if err != nil { return err } if n, _ := res.RowsAffected(); n == 0 { return ErrInvalid } if _, err := tx.Exec(`INSERT INTO sessions (grant_id) VALUES (?)`, grant); err != nil { return err } return tx.Commit() } With 100 goroutines submitting the right code at once: 1 succeeded, 1 session row, and the attempt counter read 1. The other 99 found the code already consumed and never got as far as counting an attempt. A sixth attempt with the right code after five wrong ones failed, and so did the right code after 11 minutes.
Recheck access on every download, not only at sign-in: if the share was revoked or the employee who shared it lost access, the next request fails even with a valid session.
Short sessions: 15 minutes, in a secure, HTTP-only, same-origin cookie.
Don't log codes or link tokens, and keep the invite path out of access logs and analytics.
The file is then served with byte ranges so large drawings can resume: HTTP Range requests in Go. For the approval that has to happen before anything can be shared, see multi-step document approval in Go.
Summary
- Six digits are safe only with keyed digests, binding, expiry, attempt limits, resend limits and atomic use.
- A plain SHA-256 of a 6-digit code was reversed in 62 ms; use HMAC with a server-side key.
- Count attempts before comparing and return one error for every failure.
- Consume the code and create the session together: 100 concurrent correct submissions gave one session.
- The limits cap guessing at about 0.03% per hour.
For other places where comparing secrets safely matters, see verifying webhook signatures in Node.js and JWT verification pitfalls.
Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article





