Documentation
¶
Overview ¶
Package ids generates short, URL-safe identifiers for HB Labs services services. Two flavours, both backed by `crypto/rand`:
Nonce-style strings via gonanoid (URL-safe alphabet, ~6 bits per char). Used for request-id, device-id, magic-link tokens, API tokens — anything where the value is opaque and comparable, not sortable.
ULID — 128-bit, lexicographically sortable by creation time, 26 chars in Crockford base32. Used for database primary keys so that "newest" sort orders match insertion order without an extra timestamp column.
All helpers panic on entropy-source failure. That's intentional — `crypto/rand` failing is OS-level wrong; serving a request with a non-random ID is worse than crashing.
Index ¶
Constants ¶
const ( PublicKindOrganization = "org" // tenant org PublicKindUser = "usr" // platform user PublicKindUserEmail = "em" // user email address PublicKindMembership = "mem" // user-org membership PublicKindSite = "site" // hosted site PublicKindSiteHost = "host" // site hostname PublicKindPaymentState = "pay" // payment state PublicKindMagicLink = "ml" // magic-link token PublicKindSession = "ses" // auth session PublicKindAuthIntent = "ai" // auth intent PublicKindAuthBlock = "blk" // auth block PublicKindAuthOptOut = "oo" // auth opt-out PublicKindAPIKey = "ak" // API key PublicKindInvitation = "inv" // org invitation PublicKindRequest = "req" // request log row // Ingest-site kinds. Values use the generic "is*" prefixes matching // the generalised "ingest site" model (the vertical lives in the // row's `type` field, not the ID). These replaced the legacy "dts*" // ("dog trainer site") values during the 2026-05 rename, done while // the data plane was empty so no public_id or foreign key needed // rewriting. Like every prefix here they are now part of the wire // format: add, never mutate. PublicKindIngestSite = "is" // ingest prospect site PublicKindIngestSiteFetch = "isf" // per-site crawl summary PublicKindIngestSiteFetchPage = "isfp" // per-URL fetch audit row PublicKindIngestSiteAnalysis = "isa" // per-site Stage 1b analysis row PublicKindIngestSiteResults = "isr" // per-site Stage 2 LLM results row PublicKindAppleAttestKey = "aak" // apple attest key (DCAppAttestService) PublicKindDevice = "dev" // DeviceSig Secure-Enclave device (macOS) // Mail signatures (service-signatures). PublicKindSignature = "sig" // user mail signature record PublicKindCompat = "compat" // macOS+Mail support matrix row PublicKindCompatReport = "creport" // compat-check telemetry row // Apps & feature flags (service-flags). PublicKindApp = "app" // tenant-owned native application PublicKindFeatureFlag = "ff" // scoped boolean feature flag )
PublicKind values are the 2-4 char model identifiers that sit between the "hb_" prefix and the nanoid suffix. They are part of the wire format — renaming a constant rotates every ID of that kind and is a breaking change for clients holding old values.
Keep this catalogue in sync with notes/models_consolidation_plan.md §A.11.
const DefaultLength = 21
DefaultLength is the nanoid length used by New(). 21 chars at the default URL-safe alphabet (64 symbols) gives ~126 bits of entropy — same as a UUID v4 minus the dashes, more compact, and safe in any URL or header value without escaping.
const PublicIDPrefix = "hb_"
PublicIDPrefix is the global namespace every public-facing identifier carries. The "hb_" prefix keeps platform IDs greppable in logs and lets secret scanners pick out HB Labs values from generic strings.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New() string
New returns a fresh DefaultLength-char nanoid. Suitable for request-id, device-id and any other ephemeral identifier.
func NewPublicID ¶
NewPublicID returns a typed public-facing ID of the form "hb_<kind>_<nanoid>" using DefaultLength for the nanoid suffix (~126 bits of entropy). Callers should use the PublicKind* constants instead of literal strings — typed prefixes catch "wrong kind" mistakes at code-review time, not just at runtime.
func NewSize ¶
NewSize returns a nanoid of the given length. Use 32 for tokens that need brute-force resistance (magic links, API tokens) — gives ~190 bits of entropy.
func NewSlug ¶
func NewSlug() string
NewSlug returns an 8-char random slug drawn from slugAlphabet. Collisions are guarded by a unique index at the storage layer, so the caller does not retry.
func ShortHash ¶
ShortHash returns the first 8 hex chars of the SHA-256 of data. Used to content-address stored object keys (same convention as storage-sync's file hasher), so a byte change yields a new key.
func ULID ¶
func ULID() string
ULID returns a fresh ULID string in Crockford base32 (26 chars). Use as the primary key in Postgres tables: time-sortable so the natural order of rows is also the chronological order, no clock skew issues across pods (entropy is the dominant factor between IDs created in the same millisecond), and string-cheap to compare.
func ValidatePublicID ¶
ValidatePublicID reports whether id matches the "hb_<kind>_<suffix>" shape. It is intentionally cheap: prefix + kind + separator must match, and the suffix must be non-empty. It does NOT enforce the suffix alphabet — that would couple the validator to a specific gonanoid release.
Use at API boundaries to reject mismatched kinds (eg a user_id where a site_id was expected) before the resolved row even hits the database.
Types ¶
This section is empty.