cryptoutil

package
v1.3.4 Latest Latest
Warning

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

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

Documentation

Overview

Package cryptoutil provides symmetric encryption for per-user secrets at rest: AES-256-GCM with a 96-bit random nonce, ciphertext and nonce returned as separate byte slices, 32-byte keys decoded from base64.

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.

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