JuliusBrussee/caveman · error
must decode to exactly 32 bytes, got
Error message
%s must decode to exactly 32 bytes, got %d
What it means
After base64 decoding, the key must be exactly 32 bytes for AES-256-GCM; loadKey rejects any other length with this error naming the actual decoded size. This enforces AES-256 key material at load time.
Solutions
- Generate a correctly sized key: openssl rand -base64 32 (decodes to exactly 32 bytes).
- If you must derive from a passphrase, use a KDF (e.g. scrypt/argon2) to produce 32 bytes, then base64-encode it.
- Pre-check before calling Encrypt/Decrypt: decode with base64.StdEncoding and assert len(keyBytes) == 32.
Example fix
// before export CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -hex 32) # decodes to 32 bytes? no — hex text decodes to 32 bytes only if base64 of 32 raw bytes; hex string is 64 chars -> invalid length/bytes // after export CAVE_LOCAL_ENCRYPTION_KEY=$(openssl rand -base64 32)
Defensive patterns
Strategy: validation
Validate before calling
if kb, err := base64.StdEncoding.DecodeString(os.Getenv("CAVE_LOCAL_ENCRYPTION_KEY")); err != nil || len(kb) != 32 {
return fmt.Errorf("CAVE_LOCAL_ENCRYPTION_KEY must be base64 of exactly 32 bytes (openssl rand -base64 32)")
} Try / catch
if err := secretbox.Decrypt(ct); err != nil && strings.Contains(err.Error(), "exactly 32 bytes") {
// key material has the wrong size; regenerate before proceeding
} Prevention
- Always generate keys with openssl rand -base64 32
- Never use hex strings, passphrases, or short keys as the env value
- Add the 32-byte decode check to environment smoke tests
When it happens
Trigger: Setting CAVE_LOCAL_ENCRYPTION_KEY to the base64 of a 16- or 24-byte key (AES-128/192 material), a hex string, a passphrase, or a truncated/padded base64 string that decodes to != 32 bytes.
Common situations: Reusing keys generated for a different algorithm (e.g. 16-byte keys from an older tool); passing a human password instead of derived key bytes; copy/paste truncating the base64 string.
Understand the failure class
Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.
Related errors
- aes cipher
- envelope: aes
- is not set; cannot encrypt/decrypt secrets
- is not valid base64
- secretbox: production requires CAVE_KMS_PROVIDER=scaleway
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/0e59cfe71dd91403.
Report an issue: GitHub.
Appendix: source
Thrown at shared/platform/secretbox/secretbox.go:46
"github.com/JuliusBrussee/caveman/shared/platform/runtimeenv"
)
// envKey is the name of the environment variable holding the base64-encoded
// 32-byte master key.
const envKey = "CAVE_LOCAL_ENCRYPTION_KEY"
// loadKey reads and validates the 32-byte AES key from the environment.
func loadKey() ([]byte, error) {
keyB64 := os.Getenv(envKey)
if keyB64 == "" {
return nil, fmt.Errorf("%s is not set; cannot encrypt/decrypt secrets", envKey)
}
keyBytes, err := base64.StdEncoding.DecodeString(keyB64)
if err != nil {
return nil, fmt.Errorf("%s is not valid base64: %w", envKey, err)
}
if len(keyBytes) != 32 {
return nil, fmt.Errorf("%s must decode to exactly 32 bytes, got %d", envKey, len(keyBytes))
}
return keyBytes, nil
}
// Encrypt seals plaintext with AES-256-GCM and a fresh random nonce, returning
// nonce(12) || ciphertext+tag as raw bytes.
func Encrypt(plaintext []byte) ([]byte, error) {
if useKMS() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
wrapped, err := kms.Encrypt(ctx, plaintext)
if err != nil {
return nil, fmt.Errorf("secretbox: KMS encrypt: %w", err)
}
return wrapped, nil
}
if runtimeenv.IsProduction() {
return nil, fmt.Errorf("secretbox: production requires CAVE_KMS_PROVIDER=scaleway")View on GitHub (pinned to 3ee70a1026)