crypto

package
v1.10.21 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package crypto provides AES-GCM encryption for secrets stored at rest. Init must be called once at startup with the configured key. When no key is configured, values are base64-encoded for dev convenience — never use that in production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(stored string) (string, error)

Decrypt reverses Encrypt / EncryptWS. It is self-describing: a per-workspace ("e2:w:") value resolves its DEK via the keyring; an "enc:" value uses the master key; a bare value is dev passthrough.

func DeriveToken

func DeriveToken(label string) string

DeriveToken returns a stable, high-entropy token derived from the master key via HMAC-SHA256(key, label), URL-safe base64. It lets the platform mint internal shared secrets deterministically: no separate config or storage, and every process sharing the key derives the identical value.

func DeriveTokenFrom added in v1.8.0

func DeriveTokenFrom(secret, label string) string

DeriveTokenFrom is DeriveToken for a master-key secret other than this process's. Disaster recovery needs it: after opening a backup's identity envelope the restore path must check the key inside is the one it was taken under. An empty secret yields "", never the dev fallback.

func Enabled

func Enabled() bool

Enabled reports whether a real encryption key is configured.

func Encrypt

func Encrypt(plaintext string) (string, error)

Encrypt returns the encrypted, prefixed, base64-encoded form of plaintext.

func EncryptWS

func EncryptWS(workspaceID uint, plaintext string) (string, error)

EncryptWS encrypts plaintext with the workspace's data-encryption key, producing the self-describing "e2:w:<ws>:<ver>:<b64>" form. Falls back to the master key when no keyring is wired or no master key is configured, so callers can adopt it before the keyring exists and dev still works.

func Init

func Init(secret string)

Init derives the AES-256 master key (KEK) from the secret. Empty secret disables encryption (dev). Does not clear an injected keyring.

func IsEncrypted

func IsEncrypted(s string) bool

IsEncrypted reports whether a stored value is AES-encrypted.

func LooksEncrypted

func LooksEncrypted(s string) bool

LooksEncrypted reports whether a stored value carries one of the envelope prefixes this package produces. Bare legacy plaintext returns false, so a caller can Decrypt only what it encrypted — Decrypt would otherwise base64-decode a bare value and corrupt plaintext that happens to be valid.

func Reencrypt

func Reencrypt(workspaceID uint, stored string) (string, bool, error)

Reencrypt rewrites a stored value under the workspace's current active DEK if it isn't already, and reports whether it changed. A no-op when there is no keyring, the value is empty, or it is already at the active version — so callers can run it idempotently across a workspace's rows during rotation.

func SetKeyring

func SetKeyring(kr Keyring)

SetKeyring wires the per-workspace keyring (call once at startup, after Init).

Types

type Keyring

type Keyring interface {
	// ActiveDEK returns the workspace's current key version + DEK, creating one on
	// first use.
	ActiveDEK(workspaceID uint) (version int, dek []byte, err error)
	// DEK returns a specific key version's DEK (to decrypt older ciphertext).
	DEK(workspaceID uint, version int) (dek []byte, err error)
}

Keyring resolves per-workspace data-encryption keys, unwrapping them from the master KEK. Injected at startup via SetKeyring; nil means per-workspace encryption is unavailable and EncryptWS falls back to the master key, which is safe during rollout and in dev.

func CurrentKeyring

func CurrentKeyring() Keyring

CurrentKeyring returns the wired keyring (nil if none). Lets the composition root reuse the live instance (e.g. for crypto-shred / rotation) instead of constructing a second one with a separate cache.

Jump to

Keyboard shortcuts

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