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

encrypted

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: 13 Imported by: 0

Documentation

Overview

Package encrypted provides model field types that are stored as authenticated encryption envelopes, like Laravel's encrypted casts. Generated models encrypt assigned values with the database's key ring inside the write and decrypt them while hydrating, so application code reads and assigns plaintext. Each value is bound to its table, column and row primary key: a ciphertext copied to another row, column or table does not decrypt. Values never appear in formatting, JSON, logs or audit records, and encrypted columns cannot be compared or ordered in queries because every write uses a fresh random nonce.

Index

Constants

View Source
const Purpose encryption.Purpose = "foundry.model.field.v1"

Purpose is the encryption purpose of every encrypted model field.

Variables

This section is empty.

Functions

func Binding

func Binding(table, column string, key driver.Value) (encryption.Context, error)

Binding is the authenticated context of one stored value: its table, column and row primary key, never stored in the envelope. The key is the primary key's bound SQL value, so an application-assigned key is required.

func JSONCodec

func JSONCodec[T any]() codec.Codec[JSON[T]]

JSONCodec stores the envelope of a typed JSON plaintext as text.

func TextCodec

func TextCodec() codec.Codec[Text]

TextCodec stores the envelope as text. Binding requires a sealed value, so only the framework's write path can store it; hydration yields a sealed value that the generated decoder opens. Values are sensitive and compare by plaintext.

Types

type JSON

type JSON[T any] struct {
	// contains filtered or unexported fields
}

JSON is an encrypted typed JSON field (Laravel's encrypted:array and encrypted:json casts). The plaintext is the canonical JSON of T.

func NewJSON

func NewJSON[T any](input T) (JSON[T], error)

NewJSON snapshots input through value.NewJSON before it is stored.

func (JSON[T]) Decode

func (j JSON[T]) Decode() (T, error)

Decode returns a fresh T from the plaintext JSON with value.ParseJSON's strict shape checks.

func (JSON[T]) Equal

func (j JSON[T]) Equal(other JSON[T]) bool

Equal compares plaintexts when both values are open, otherwise matching nonzero envelopes.

func (JSON[T]) Format

func (JSON[T]) Format(s fmt.State, _ rune)

Format writes the redaction marker without exposing plaintext or ciphertext.

func (JSON[T]) GoString

func (j JSON[T]) GoString() string

GoString returns the redaction marker.

func (JSON[T]) IsZero

func (j JSON[T]) IsZero() bool

IsZero reports a value with neither plaintext nor a stored envelope.

func (JSON[T]) LogValue

func (JSON[T]) LogValue() slog.Value

LogValue returns the redaction marker.

func (JSON[T]) MarshalJSON

func (JSON[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes the redaction marker as a JSON string.

func (JSON[T]) MarshalText

func (JSON[T]) MarshalText() ([]byte, error)

MarshalText returns the redaction marker.

func (JSON[T]) Open

func (j JSON[T]) Open(ctx context.Context, keys *encryption.Keyring, binding encryption.Context) (JSON[T], error)

Open decrypts the stored envelope for binding and checks that the plaintext fits T. It returns a zero value on failure.

func (JSON[T]) Seal

func (j JSON[T]) Seal(ctx context.Context, keys *encryption.Keyring, binding encryption.Context) (JSON[T], error)

Seal returns a copy with a fresh encrypted envelope for binding. A nil key ring returns fault.Missing; absent plaintext returns fault.Invalid.

type Text

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

Text is an encrypted string field (Laravel's encrypted cast). Assign NewText and read Reveal. The zero value has no plaintext and cannot be stored.

func NewText

func NewText(plain string) Text

NewText returns an assigned plaintext value, including for an empty string.

func (Text) Equal

func (t Text) Equal(other Text) bool

Equal compares plaintexts when both values are open, otherwise matching nonzero envelopes.

func (Text) Format

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

Format writes the redaction marker without exposing plaintext or ciphertext.

func (Text) GoString

func (t Text) GoString() string

GoString returns the redaction marker.

func (Text) IsZero

func (t Text) IsZero() bool

IsZero reports a value with neither plaintext nor a stored envelope.

func (Text) LogValue

func (Text) LogValue() slog.Value

LogValue returns the redaction marker.

func (Text) MarshalJSON

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

MarshalJSON encodes the redaction marker as a JSON string.

func (Text) MarshalText

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

MarshalText returns the redaction marker.

func (Text) Open

func (t Text) Open(ctx context.Context, keys *encryption.Keyring, binding encryption.Context) (Text, error)

Open returns a copy with plaintext decrypted from the stored envelope for binding. A nil key ring returns fault.Missing; a missing envelope returns fault.Invalid.

func (Text) Reveal

func (t Text) Reveal() string

Reveal returns the plaintext of an assigned or hydrated value; a value that was never opened reveals nothing.

func (Text) Seal

func (t Text) Seal(ctx context.Context, keys *encryption.Keyring, binding encryption.Context) (Text, error)

Seal returns a copy with a fresh encrypted envelope for binding. A nil key ring returns fault.Missing; absent plaintext returns fault.Invalid.

type Value

type Value[V any] interface {
	// Seal encrypts assigned plaintext for the supplied record binding.
	Seal(context.Context, *encryption.Keyring, encryption.Context) (V, error)
	// Open decrypts a stored envelope for the supplied record binding.
	Open(context.Context, *encryption.Keyring, encryption.Context) (V, error)
	// Equal compares values for stored-change detection.
	Equal(V) bool
}

Value is implemented by encrypted field types. Seal encrypts an assigned plaintext for one record; Open decrypts a stored envelope for one record; Equal compares plaintexts for change detection. Generated code calls these; applications assign and read plaintext through each type's own methods.

Jump to

Keyboard shortcuts

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