crypto

package
v0.9.17 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKey       = errors.New("invalid encryption key")
	ErrInvalidPayload   = errors.New("invalid payload format")
	ErrDecryptionFailed = errors.New("decryption failed")
	ErrInvalidCipher    = errors.New("unsupported cipher")
	ErrNotInitialized   = errors.New("encryptor not initialized")
)

Errors

Functions

func SerializePayload

func SerializePayload(p *Payload) (string, error)

SerializePayload converts a payload to base64 JSON

Types

type Config

type Config struct {
	Key          string   // Primary encryption key
	PreviousKeys []string // Previous keys for rotation
	Cipher       string   // Cipher algorithm
}

Config holds encryption configuration

func ConfigFromEnv added in v0.9.5

func ConfigFromEnv() (Config, bool)

ConfigFromEnv builds a Config from environment variables. It reads CRYPTO_KEY (or APP_KEY), CRYPTO_CIPHER, and CRYPTO_OLD_KEYS. Returns the config and true if a key was found, or a zero Config and false otherwise.

type Encryptor

type Encryptor interface {
	// Encrypt encrypts plaintext and returns a base64 encoded payload
	Encrypt(plaintext string) (string, error)

	// EncryptBytes encrypts bytes and returns a base64 encoded payload
	EncryptBytes(plaintext []byte) (string, error)

	// Decrypt decrypts a base64 encoded payload and returns plaintext
	Decrypt(payload string) (string, error)

	// DecryptBytes decrypts a base64 encoded payload and returns bytes
	DecryptBytes(payload string) ([]byte, error)

	// GenerateKey generates a new encryption key for the cipher
	GenerateKey() (string, error)
}

Encryptor interface defines encryption operations

func NewEncryptor

func NewEncryptor(config Config) (Encryptor, error)

NewEncryptor creates a new encryptor with custom configuration

type Payload

type Payload struct {
	IV    string `json:"iv"`            // Initialization vector (base64)
	Value string `json:"value"`         // Encrypted value (base64)
	MAC   string `json:"mac,omitempty"` // HMAC for CBC modes (base64)
	Tag   string `json:"tag,omitempty"` // Authentication tag for GCM modes (base64)
}

Payload represents the encrypted data structure

func DeserializePayload

func DeserializePayload(encoded string) (*Payload, error)

DeserializePayload converts base64 JSON to a payload

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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