Documentation
¶
Index ¶
- Variables
- func Decrypt(payload string) (string, error)
- func DecryptBytes(payload string) ([]byte, error)
- func Encrypt(plaintext string) (string, error)
- func EncryptBytes(plaintext []byte) (string, error)
- func GenerateKey() (string, error)
- func Init(config Config) error
- func SerializePayload(p *Payload) (string, error)
- type Config
- type Encryptor
- type Payload
Constants ¶
This section is empty.
Variables ¶
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 DecryptBytes ¶
DecryptBytes decrypts a payload using the global encryptor.
func EncryptBytes ¶
EncryptBytes encrypts bytes using the global encryptor.
func GenerateKey ¶
GenerateKey generates a new encryption key for the current cipher.
func SerializePayload ¶
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
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 ¶
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 ¶
DeserializePayload converts base64 JSON to a payload