crypto

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package crypto provides encryption and decryption for sensitive data.

Index

Constants

View Source
const (
	// EncryptedPrefix is prepended to encrypted values to identify them.
	EncryptedPrefix = "enc:v1:"
)

Variables

View Source
var (
	// ErrInvalidCiphertext indicates the ciphertext is malformed.
	ErrInvalidCiphertext = errors.New("invalid ciphertext")

	// ErrNoEncryptionKey indicates no encryption key is available.
	ErrNoEncryptionKey = errors.New("no encryption key configured")
)

Functions

func Decrypt

func Decrypt(ciphertext string) (string, error)

Decrypt decrypts ciphertext using the default encryptor.

func Encrypt

func Encrypt(plaintext string) (string, error)

Encrypt encrypts plaintext using the default encryptor.

func IsEncrypted

func IsEncrypted(value string) bool

IsEncrypted checks if a value is encrypted (has the encrypted prefix).

Types

type Encryptor

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

Encryptor handles encryption and decryption of sensitive data.

func Default

func Default() *Encryptor

Default returns the default Encryptor instance, initializing it if necessary. The encryption key is derived from the ENCRYPTION_KEY environment variable, or falls back to a machine-specific key if not set.

func New

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

New creates a new Encryptor with the given key. Key must be exactly 32 bytes for AES-256.

func (*Encryptor) Decrypt

func (e *Encryptor) Decrypt(ciphertext string) (string, error)

Decrypt decrypts a ciphertext that was encrypted with Encrypt. If the value doesn't have the encrypted prefix, it's returned as-is (legacy support).

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt(plaintext string) (string, error)

Encrypt encrypts plaintext and returns a base64-encoded ciphertext with prefix.

Jump to

Keyboard shortcuts

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