Files
archie/internal/pairing/hash.go
T
sam f73b2d1c8c feat: memory manager sync/prefetch/scan/shutdown + broad in-flight epic work
Checkpoint commit covering concurrent work across several archie-core-abg
epics to avoid losing in-progress state:

- Memory manager (18.7-18.16): background sync pipeline, prefetch with
  timeout, memory-write notification, threat scanning, shutdown drain
  with abandoned-write tracking, frozen snapshot pattern. Wired
  ScanContent and NotifyMemoryToolWrite into HandleToolCall so scanning
  and notification actually apply to writes, not just exist as unused
  primitives.
- Fixed a literal unresolved merge-conflict that had been committed to
  internal/memory/scrubber/scrubber.go.
- Tool provider/registry, MCP client/transport (stdio, HTTP, SSE),
  multimodal results, disclosure.
- Pairing device auth store, rate limiting.
- Gateway compression, session store, tasks.
- Various workflow, plugin host, and agentexec updates.
2026-07-27 03:34:46 +10:00

50 lines
1.5 KiB
Go

package pairing
import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"fmt"
)
// saltLen is the size in bytes of a generated salt. 16 bytes (128 bits)
// is standard practice for a salt -- large enough that two salts
// colliding is negligible, without adding meaningful storage cost.
const saltLen = 16
// HashedCode is a salted SHA-256 digest of a pairing code, safe to store
// (the plaintext code itself never needs to be persisted after hashing).
type HashedCode struct {
Salt []byte
Hash []byte
}
// HashCode generates a random salt and returns the salted SHA-256 digest
// of code. Each call produces a different salt (and therefore a
// different Hash) even for the same code.
func HashCode(code string) (HashedCode, error) {
salt := make([]byte, saltLen)
if _, err := rand.Read(salt); err != nil {
return HashedCode{}, fmt.Errorf("pairing: generate salt: %w", err)
}
return HashedCode{Salt: salt, Hash: digest(salt, code)}, nil
}
// Verify reports whether code hashes to the same digest as h, using a
// constant-time comparison so the check doesn't leak timing information
// about how much of the digest matched.
func (h HashedCode) Verify(code string) bool {
if len(h.Salt) == 0 || len(h.Hash) == 0 {
return false
}
return subtle.ConstantTimeCompare(h.Hash, digest(h.Salt, code)) == 1
}
// digest computes SHA-256(salt || code).
func digest(salt []byte, code string) []byte {
sum := sha256.New()
sum.Write(salt)
sum.Write([]byte(code))
return sum.Sum(nil)
}