crypto

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 7 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 Decrypt

func Decrypt(payload string) (string, error)

Decrypt decrypts a payload using the global encryptor

func DecryptBytes

func DecryptBytes(payload string) ([]byte, error)

DecryptBytes decrypts a payload using the global encryptor

func Encrypt

func Encrypt(plaintext string) (string, error)

Encrypt encrypts plaintext using the global encryptor

func EncryptBytes

func EncryptBytes(plaintext []byte) (string, error)

EncryptBytes encrypts bytes using the global encryptor

func GenerateKey

func GenerateKey() (string, error)

GenerateKey generates a new encryption key for the current cipher

func Init

func Init(config Config) error

Init initializes the global encryptor

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

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