Help us shape the future of Go! Take the 2026 Go Developer Survey

encryption

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package encryption provides bounded, versioned AES-256-GCM envelopes using Go's random-nonce AEAD. Keys are explicit application configuration; the package never generates a replacement for a missing decryption key. Bind each value to its purpose and owning record through Context. It does not provide streaming encryption, key custody or a distributed key-usage counter.

Index

Constants

View Source
const MaxBindingBytes = 8192
View Source
const MaxKeys = 32
View Source
const MaxPlaintextBytes = 1 << 20

Variables

This section is empty.

Functions

func EnvelopePrefix

func EnvelopePrefix(id KeyID) (string, error)

EnvelopePrefix returns the text every envelope encrypted under id starts with. Storage adapters use it to select records that still use another key during rotation; it is not secret and never authenticates anything.

Types

type Ciphertext

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

Ciphertext is a bounded, canonical versioned envelope. Parsing validates its structure only; authentication happens in Keyring.Decrypt. Encoded is the explicit persistence boundary. Ordinary formatting and JSON redact it.

func ParseCiphertext

func ParseCiphertext(encoded string) (Ciphertext, error)

ParseCiphertext validates the version, key identifier, canonical base64 encoding, and size of a ciphertext envelope.

func (Ciphertext) Encoded

func (c Ciphertext) Encoded() string

Encoded returns the encoded ciphertext envelope.

func (Ciphertext) Format

func (Ciphertext) Format(s fmt.State, _ rune)

Format writes the redaction marker instead of ciphertext.

func (Ciphertext) IsZero

func (c Ciphertext) IsZero() bool

IsZero reports whether the ciphertext is uninitialized.

func (Ciphertext) KeyID

func (c Ciphertext) KeyID() KeyID

KeyID returns the envelope's key identifier.

func (Ciphertext) LogValue

func (Ciphertext) LogValue() slog.Value

LogValue returns the redaction marker for logging.

func (Ciphertext) MarshalJSON

func (Ciphertext) MarshalJSON() ([]byte, error)

MarshalJSON encodes the redaction marker instead of ciphertext.

type Context

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

Context authenticates a purpose and exact owning-record binding alongside an envelope. It is not stored in the envelope and must be reconstructed from trusted record identity at decryption. Include tenant and record generation when relevant. Empty bindings are rejected.

func NewContext

func NewContext(purpose Purpose, binding secret.String) (Context, error)

NewContext constructs a context from a semantic purpose and a nonempty, bounded binding.

func (Context) Format

func (Context) Format(s fmt.State, _ rune)

Format writes a fixed label without exposing the binding.

func (Context) LogValue

func (Context) LogValue() slog.Value

LogValue returns a fixed log label without exposing the binding.

func (Context) MarshalJSON

func (Context) MarshalJSON() ([]byte, error)

MarshalJSON encodes the redaction marker instead of context data.

func (Context) Validate

func (c Context) Validate() error

Validate validates the purpose and binding using NewContext.

type Key

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

Key owns one immutable AES-256 key and a local encryption budget shared by copies and keyrings. Loading the same material again or on another process creates a separate counter: operators must rotate before aggregate use across all instances/restarts exceeds 2^32 encryptions with the same material.

func GenerateKey

func GenerateKey(id KeyID) (Key, error)

GenerateKey creates a key for explicit installation in a secret store. Persist its Secret before encrypting durable data; do not generate keys at each boot.

func ParseKey

func ParseKey(id KeyID, encoded secret.String) (Key, error)

ParseKey accepts exactly 32 bytes encoded as canonical unpadded base64url. It performs no I/O and never logs or includes key material in errors.

func (Key) Format

func (Key) Format(s fmt.State, _ rune)

Format writes the redaction marker instead of key material.

func (Key) ID

func (k Key) ID() KeyID

ID returns the key identifier, or an empty identifier for an uninitialized key.

func (Key) LogValue

func (Key) LogValue() slog.Value

LogValue returns the redaction marker for logging.

func (Key) MarshalJSON

func (Key) MarshalJSON() ([]byte, error)

MarshalJSON encodes the redaction marker instead of key material.

func (Key) Secret

func (k Key) Secret() secret.String

Secret returns base64-encoded key material as a secret, or an empty secret for an uninitialized key.

type KeyID

type KeyID string

KeyID is a non-secret stable name persisted in encrypted envelopes. Never change the material assigned to an existing ID.

type Keyring

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

Keyring encrypts with one active key and decrypts with its retained keys. It is immutable and safe for concurrent use. Retain old keys until every record has been re-encrypted; unknown IDs never fall back to the active key.

func NewKeyring

func NewKeyring(active KeyID, keys ...Key) (*Keyring, error)

NewKeyring constructs a bounded keyring, rejecting duplicate identifiers or key material and a missing active key.

func (*Keyring) ActiveID

func (r *Keyring) ActiveID() KeyID

ActiveID returns the active key identifier, or an empty identifier for an uninitialized keyring.

func (*Keyring) Decrypt

func (r *Keyring) Decrypt(ctx context.Context, binding Context, ciphertext Ciphertext) (secret.String, error)

Decrypt returns no plaintext on any failure. Missing keys and failed authentication share one error. It never accepts legacy or unauthenticated formats, retries other keys, or silently replaces a missing key.

func (*Keyring) Encrypt

func (r *Keyring) Encrypt(ctx context.Context, binding Context, plaintext secret.String) (Ciphertext, error)

Encrypt owns its output and generates its nonce in Go's standard AEAD. Context checks bound entry/exit; the bounded local cipher operation is not preemptible. An admitted encryption consumes budget even if cancellation discards output.

func (Keyring) Format

func (Keyring) Format(s fmt.State, _ rune)

Format writes a fixed label without exposing keys.

func (Keyring) LogValue

func (Keyring) LogValue() slog.Value

LogValue returns a fixed log label without exposing keys.

func (Keyring) MarshalJSON

func (Keyring) MarshalJSON() ([]byte, error)

MarshalJSON encodes the redaction marker instead of keyring contents.

func (*Keyring) Reencrypt

func (r *Keyring) Reencrypt(ctx context.Context, binding Context, ciphertext Ciphertext) (Ciphertext, error)

Reencrypt authenticates with the retained source key and creates a fresh envelope using the active key, preserving the exact context and plaintext. Callers own atomic record replacement; this method does no persistence.

func (*Keyring) Validate

func (r *Keyring) Validate() error

Validate checks that the keyring has an active key.

type Purpose

type Purpose string

Purpose separates uses of encryption, for example auth.mfa.totp.v1.

Jump to

Keyboard shortcuts

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