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 ¶
- func Decrypt(stored string) (string, error)
- func DeriveToken(label string) string
- func DeriveTokenFrom(secret, label string) string
- func Enabled() bool
- func Encrypt(plaintext string) (string, error)
- func EncryptWS(workspaceID uint, plaintext string) (string, error)
- func Init(secret string)
- func IsEncrypted(s string) bool
- func LooksEncrypted(s string) bool
- func Reencrypt(workspaceID uint, stored string) (string, bool, error)
- func SetKeyring(kr Keyring)
- type Keyring
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decrypt ¶
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 ¶
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
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 EncryptWS ¶
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 ¶
IsEncrypted reports whether a stored value is AES-encrypted.
func LooksEncrypted ¶
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 ¶
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.