secret

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package secret holds values that must never reach a log: passwords, keys, tokens. Every way of printing one (fmt, slog, JSON) writes [redacted]; only an explicit Reveal gives the value, at the one place that needs it.

A Keyring seals values under the instance's secret key for storage, so only an instance holding the key reads them back, and derives the keys of other uses from it.

Index

Constants

View Source
const Base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Base62Alphabet is the alphabet credential values are drawn from: letters and digits, safe in a header, a URL and a shell without quoting.

View Source
const MinKeyLength = 32

MinKeyLength is the fewest bytes a secret key may have. Every key the keyring uses is derived from the secret key, and derivation adds no strength: a short key would make sealed secrets and session signatures guessable.

Variables

View Source
var ErrCannotOpen = errors.New("the sealed value cannot be opened")

ErrCannotOpen is every failure to open a sealed value alike: another key, another purpose, a tampered value or no sealed value at all, since telling them apart would help whoever tampers.

View Source
var ErrSecretKeyChanged = errors.New("FLAGNETIC_SECRET_KEY does not open this instance's canary: " +
	"it is not the key the instance was set up with, and what the instance sealed cannot be read without that key; " +
	"start with the original key")

ErrSecretKeyChanged is a secret key that does not open the canary the instance stored: not the key the instance was set up with.

Functions

func Base62

func Base62(random io.Reader, n int) string

Base62 draws n characters of Base62Alphabet from random without bias: a byte is used only below 248, the largest multiple of 62 a byte holds, since reducing the bytes above it would favour the first characters. It is the one generator of the platform's credential values.

func IsBase62

func IsBase62(s string) bool

IsBase62 reports whether s holds only characters of Base62Alphabet.

Types

type Keyring

type Keyring struct {
	// contains filtered or unexported fields
}

Keyring seals and opens values, and derives keys, under the instance's secret key.

func NewKeyring

func NewKeyring(secretKey String) *Keyring

NewKeyring returns the keyring of secretKey.

func (*Keyring) CheckCanary

func (k *Keyring) CheckCanary(canary string) error

CheckCanary returns ErrSecretKeyChanged unless the secret key opens canary.

func (*Keyring) Key

func (k *Keyring) Key(use Use) []byte

Key returns the key derived for use, the same for the same secret key.

func (*Keyring) NewCanary

func (k *Keyring) NewCanary() (string, error)

NewCanary returns a new canary: random bytes sealed under the secret key, stored once by the instance and opened on every start.

func (*Keyring) Open

func (k *Keyring) Open(purpose Purpose, sealed string) ([]byte, error)

Open returns the plaintext of a value sealed for purpose, or ErrCannotOpen.

func (*Keyring) Seal

func (k *Keyring) Seal(purpose Purpose, plaintext []byte) (string, error)

Seal encrypts plaintext for purpose with AES-256-GCM under a fresh random nonce, and returns "v1:" and the base64url of the nonce and the ciphertext.

type Purpose

type Purpose string

Purpose names what a sealed value is for. Each purpose seals under a key of its own, so a value sealed for one purpose never opens as another.

const (
	// PurposeCanary seals the canary.
	PurposeCanary Purpose = "canary"
	// PurposeWebhookSecret seals the secrets signing webhook calls.
	PurposeWebhookSecret Purpose = "webhook secret"
)

The purposes values are sealed for.

type String

type String struct {
	// contains filtered or unexported fields
}

String is a secret string. Its zero value is the empty secret.

func New

func New(v string) String

New wraps v.

func (String) GoString

func (String) GoString() string

GoString returns [redacted], for %#v.

func (String) IsZero

func (s String) IsZero() bool

IsZero reports whether the secret is empty.

func (String) LogValue

func (String) LogValue() slog.Value

LogValue returns [redacted].

func (String) MarshalText

func (String) MarshalText() ([]byte, error)

MarshalText returns [redacted], so an encoded configuration hides it too.

func (String) Reveal

func (s String) Reveal() string

Reveal returns the value. Call it only where the value is used, never to log.

func (String) String

func (String) String() string

String returns [redacted].

type Use

type Use string

Use names a key derived from the secret key for something other than sealing.

const UseSessionCookie Use = "session cookie"

UseSessionCookie keys the HMAC signing session cookies.

Jump to

Keyboard shortcuts

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