encrypted

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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

func (JSON[T]) Format

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

func (JSON[T]) GoString

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

func (JSON[T]) IsZero

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

func (JSON[T]) LogValue

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

func (JSON[T]) MarshalJSON

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

func (JSON[T]) MarshalText

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

func (JSON[T]) Open

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

func (JSON[T]) Seal

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

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

func (Text) Equal

func (t Text) Equal(other Text) bool

func (Text) Format

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

func (Text) GoString

func (t Text) GoString() string

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

func (Text) MarshalJSON

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

func (Text) MarshalText

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

func (Text) Open

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

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)

type Value

type Value[V any] interface {
	Seal(context.Context, *encryption.Keyring, encryption.Context) (V, error)
	Open(context.Context, *encryption.Keyring, encryption.Context) (V, error)
	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