secrets

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package secrets provides AES-256-GCM encryption for application secrets. Secrets are encrypted at rest and loaded from environment variables or an encrypted .secrets file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(key []byte, encoded string) ([]byte, error)

Decrypt decrypts a base64url-encoded ciphertext produced by Encrypt.

func Encrypt

func Encrypt(key, plaintext []byte) (string, error)

Encrypt encrypts plaintext using AES-256-GCM with the given 32-byte key. The returned string is URL-safe base64: nonce (12 bytes) || ciphertext || tag.

func GenerateKey

func GenerateKey() (string, error)

GenerateKey generates a new cryptographically random 32-byte key and returns it in the "base64:<std-base64>" form suitable for APP_KEY.

func ParseKey

func ParseKey(s string) ([]byte, error)

ParseKey decodes an APP_KEY value into a 32-byte key. A "base64:"-prefixed value (as produced by GenerateKey) is base64-decoded and must be exactly 32 bytes; any other value is treated as a passphrase and stretched to 32 bytes with scrypt.

Types

type Manager

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

Manager holds and provides access to application secrets. Secrets can be encrypted at rest and decrypted on demand using the app key.

func New

func New(appKey string) (*Manager, error)

New creates a Manager. appKey is the raw application secret (e.g. APP_KEY env var). If appKey is empty, a random EPHEMERAL key is generated (useful for testing): anything encrypted with it is unrecoverable after the process exits, so a loud warning is logged.

func (*Manager) Decrypt

func (m *Manager) Decrypt(ciphertext string) (string, error)

Decrypt decrypts a base64url-encoded ciphertext produced by Encrypt.

plain, err := manager.Decrypt(encrypted)

func (*Manager) Encrypt

func (m *Manager) Encrypt(plaintext string) (string, error)

Encrypt encrypts plaintext using AES-256-GCM and returns a base64url-encoded ciphertext.

encrypted, err := manager.Encrypt("my-secret-password")

func (*Manager) Env

func (m *Manager) Env(name, envVar string)

Env loads a secret from an environment variable, storing it under name.

func (*Manager) Get

func (m *Manager) Get(name string) (string, bool)

Get retrieves a secret by name.

func (*Manager) Must

func (m *Manager) Must(name string) string

Must retrieves a secret or panics if not found.

func (*Manager) Set

func (m *Manager) Set(name, value string)

Set stores a secret value under name (plain-text stored in memory only).

Jump to

Keyboard shortcuts

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