crypto

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package crypto provides AES-256-GCM encryption of secrets stored at rest (webhook secrets, upstream passwords, SSO client secrets, AI API keys, storage backend credentials).

The key comes from the OPENARTIFACT_SECRET_KEY environment variable (any string of at least 16 bytes). Two ciphertext generations exist, both encoded as "<prefix><base64(nonce||sealed)>":

  • "enc:v2:" (written by Encrypt): the AES-256 key is derived with HKDF-SHA256 (RFC 5869) from the configured secret, using the fixed salt [hkdfSalt] and the context string [hkdfInfo]. The salt is a constant because the secret is the only per-deployment input; it separates this derivation from any other use of the same string.
  • "enc:v1:" (legacy, read-only): the AES-256 key is the plain SHA-256 of the configured secret. Decrypt still opens these transparently; values are re-sealed as v2 the next time they are saved. There is no forced migration.

An older binary that only knows "enc:v1:" cannot read "enc:v2:" values, so downgrading after v2 values were written requires re-entering those secrets.

HKDF is a key-derivation function, not a password hash: it does not slow down guessing, so the secret should be high-entropy (for example `openssl rand -base64 32`).

Values without a known prefix are treated as legacy plaintext by Decrypt. When no key is configured, Encrypt returns the plaintext unchanged (and Enabled() is false), so deployments without a key keep working but store secrets unprotected.

Index

Constants

View Source
const EnvKey = "OPENARTIFACT_SECRET_KEY"

EnvKey is the environment variable holding the secret-encryption key.

View Source
const MinKeyLen = 16

MinKeyLen is the minimum accepted key length in bytes.

Variables

View Source
var ErrEncryptedInput = errors.New("crypto: value must be plaintext, not an encrypted value")

ErrEncryptedInput is returned by CheckPlaintext for values that already carry the ciphertext prefix.

View Source
var ErrNoKey = errors.New("crypto: encrypted value present but " + EnvKey + " is not set")

ErrNoKey is returned by Decrypt when ciphertext is present but no key is configured.

Functions

func CheckPlaintext added in v0.6.0

func CheckPlaintext(s string) error

CheckPlaintext rejects user-supplied secret input that looks like a ciphertext produced by Encrypt. Call it at every API boundary that accepts a secret to be stored encrypted.

func Decrypt

func Decrypt(value string) (string, error)

Decrypt opens an "enc:v2:" or legacy "enc:v1:" value produced by Encrypt. Values without a known prefix are legacy plaintext and returned as-is.

func Enabled

func Enabled() bool

Enabled reports whether a key is configured.

func Encrypt

func Encrypt(plaintext string) (string, error)

Encrypt seals plaintext as an "enc:v2:" value. Empty input is returned unchanged; without a configured key the plaintext is returned unchanged. Input is always sealed, even when it already carries an "enc:" prefix: passing such input through would let a caller smuggle a ciphertext into a field that is later decrypted and used (a decryption oracle). API boundaries must additionally reject prefixed input with ErrEncryptedInput.

func Init

func Init(key string) error

Init configures the package-level key. An empty key disables encryption.

func InitFromEnv

func InitFromEnv() error

InitFromEnv configures the key from OPENARTIFACT_SECRET_KEY.

func IsEncrypted

func IsEncrypted(s string) bool

IsEncrypted reports whether s is an encrypted value produced by Encrypt. Both the enc:v1: and enc:v2: generations are recognised.

Types

This section is empty.

Jump to

Keyboard shortcuts

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