keygen

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package keygen generates synthetic test keys from registry patterns (AD-16).

Every key matches its detector's regex and carries the TOKENHOUNDTEST marker, placed or encoded as the pattern's test_marker declares, so findings from test keys are recognisable and suppressed by default. Keys are generated at test time only and are never written into the repository.

Alternatives are chosen at random, except that an empty-width alternative (^, $, \b, empty) always wins, so boundary groups add no character to a key.

Output depends only on the registry, the detector id and the seed: the PRNG is math/rand/v2 PCG, so the same seed yields byte-identical keys on every OS.

Index

Constants

View Source
const Marker = "TOKENHOUNDTEST"

Marker is the plain-text marker every generated key carries.

Variables

View Source
var (
	// ErrUnknownDetector: the id names no provider or detector in the registry.
	ErrUnknownDetector = errors.New("keygen: unknown detector id")
	// ErrNoMarkerSlot: no character-class repeat in the pattern can hold the marker
	// in the declared encoding.
	ErrNoMarkerSlot = errors.New("keygen: pattern has no character-class repeat that can hold the marker")
	// ErrUnsupportedRegex: the regex uses a construct the generator cannot produce.
	ErrUnsupportedRegex = errors.New("keygen: unsupported regex construct")
	// ErrNoMatch: the generated key does not match its own regex.
	ErrNoMatch = errors.New("keygen: generated key does not match its regex")
	// ErrTemplate: the fixture template is malformed.
	ErrTemplate = errors.New("keygen: invalid template")
)

Errors returned by Key and Expand.

Functions

func B64

func B64(s string) string

B64 is standard base64 with padding.

func B64URL

func B64URL(s string) string

B64URL is URL-safe base64 without padding.

func Expand

func Expand(reg *registry.Registry, tmpl string, seed uint64) (string, error)

Expand executes a fixture template. {{key "<id>"}} expands to Key(reg, id, seed); {{key "<id>" N}} yields a different key for each variant N, so one fixture can hold two distinct keys of the same detector. The same id and variant always expand to the same key within one seed.

{{keypart "<provider>" "<part>"}} expands to the key of a credential part, by the part name the registry declares ({{keypart "aws" "secret_access_key"}}); it takes the same optional variant N and is the same key as {{key}} of the part's pattern.

Encoding functions wrap a key for decode fixtures: {{b64 (key "openai")}} is standard padded base64, b64url URL-safe base64 without padding, pct percent-encodes every byte as %XX, and jsonesc writes every - and _ as a \u00XX JSON escape and every / as \/. They nest: {{b64 (b64 (key "openai"))}}.

func HasMarker

func HasMarker(key string) bool

HasMarker reports whether key carries the marker in any encoding.

func JSONEsc

func JSONEsc(s string) string

JSONEsc writes every - and _ of s as a \u00XX JSON escape and every / as \/.

func Key

func Key(reg *registry.Registry, id string, seed uint64) (string, error)

Key returns a synthetic key for id, which is a detector id ("openai.project-key") or a provider id or alias ("openai", meaning the provider's first pattern in file order). The same registry, id and seed always yield the same key.

func KeyPart

func KeyPart(reg *registry.Registry, provider, part string, seed uint64) (string, error)

KeyPart returns a synthetic key for the credential part named part of provider (KeyPart(reg, "aws", "secret_access_key", seed)): the key of the part's pattern.

func MarkerFor

func MarkerFor(pat registry.Pattern) []string

MarkerFor returns the marker as it appears in keys for pat: plain, or hex encoded (lowercase; uppercase hex is used when the pattern only allows it).

func Pct

func Pct(s string) string

Pct percent-encodes every byte of s as %XX (uppercase hex).

Types

type UnknownDetectorError

type UnknownDetectorError struct {
	ID string
}

UnknownDetectorError names the id that was not found. It wraps ErrUnknownDetector.

func (*UnknownDetectorError) Error

func (e *UnknownDetectorError) Error() string

func (*UnknownDetectorError) Unwrap

func (e *UnknownDetectorError) Unwrap() error

Unwrap returns ErrUnknownDetector.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL