cryptoutil

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cryptoutil provides symmetric encryption primitives used to protect per-user secrets (currently: cloud backup credentials) at rest.

Design goals:

  • One key, one algorithm — AES-256-GCM with a 96-bit random nonce.
  • Tampering and nonce-reuse are detectable (GCM authentication tag).
  • Caller-friendly: ciphertext and nonce are returned as separate byte slices so the storage layer can put each in its own column.
  • Key handling: the key is loaded from a base64-encoded environment variable at startup; KeyFromBase64 enforces a 32-byte key length and refuses obviously-empty values to fail closed on a misconfigured deployment.

Index

Constants

View Source
const KeySize = 32

AES-256 key size in bytes.

View Source
const NonceSize = 12

GCM nonce size in bytes (96 bits; the standard recommendation).

Variables

View Source
var (
	// ErrInvalidKey is returned when the supplied key is not exactly 32 bytes.
	ErrInvalidKey = errors.New("cryptoutil: key must be 32 bytes (AES-256)")
	// ErrInvalidCiphertext is returned when the ciphertext fails authentication
	// or is malformed.
	ErrInvalidCiphertext = errors.New("cryptoutil: ciphertext could not be decrypted")
)

Functions

func DecodeKey

func DecodeKey(encoded string) ([]byte, error)

DecodeKey decodes a base64-encoded key and validates length.

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey returns a cryptographically random 32-byte key suitable for AES-256. Intended for development tooling and tests.

func GenerateKeyBase64

func GenerateKeyBase64() (string, error)

GenerateKeyBase64 returns GenerateKey encoded as standard base64.

Types

type AEAD

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

AEAD wraps an AES-256-GCM cipher with helper methods. Construct via New or NewFromBase64.

func New

func New(key []byte) (*AEAD, error)

New constructs an AEAD from a 32-byte key.

func NewFromBase64

func NewFromBase64(encoded string) (*AEAD, error)

NewFromBase64 decodes a standard or URL-safe base64 string and constructs an AEAD. Spaces in the input are ignored to tolerate copy-paste artifacts.

func (*AEAD) Decrypt

func (a *AEAD) Decrypt(ciphertext, nonce []byte) ([]byte, error)

Decrypt authenticates and decrypts ciphertext produced by Encrypt.

func (*AEAD) Encrypt

func (a *AEAD) Encrypt(plaintext []byte) (ciphertext, nonce []byte, err error)

Encrypt seals plaintext with a fresh random nonce. Returns (ciphertext, nonce, error). The 16-byte authentication tag is appended to the ciphertext by the underlying GCM implementation.

Jump to

Keyboard shortcuts

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