Documentation
¶
Overview ¶
Package encryption provides AES-256 encryption/decryption capabilities for user preferences. It includes secure key validation and environment variable management.
Index ¶
Constants ¶
const ( // MinKeyLength is the minimum required length for the input key material (32 bytes). // The actual AES key will be derived from this using SHA-256. MinKeyLength = 32 // AESKeyLength is the required length for AES-256 keys (32 bytes). AESKeyLength = 32 // EnvKeyName is the environment variable name for the encryption key. EnvKeyName = "USERPREFS_ENCRYPTION_KEY" )
Variables ¶
var ( // ErrInvalidKeyLength is returned when the encryption key doesn't meet minimum length requirements. ErrInvalidKeyLength = errors.New("encryption key must be at least 32 bytes") // ErrKeyNotFound is returned when the encryption key environment variable is not set. ErrKeyNotFound = errors.New("encryption key not found in environment variable " + EnvKeyName) // ErrEncryptionFailed is returned when encryption operation fails. ErrEncryptionFailed = errors.New("encryption operation failed") // ErrDecryptionFailed is returned when decryption operation fails. ErrDecryptionFailed = errors.New("decryption operation failed") // ErrInvalidCiphertext is returned when the ciphertext is malformed or too short. ErrInvalidCiphertext = errors.New("invalid ciphertext: too short or malformed") )
Functions ¶
func ValidateKey ¶
func ValidateKey() error
ValidateKey validates that the encryption key meets security requirements. This can be called early in application startup for fast-fail validation.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles AES-256-GCM encryption and decryption operations. It validates the encryption key during initialization for fast-fail scenarios.
func NewManager ¶
NewManager creates a new encryption manager with the key from environment variable. It validates the key strength and length during initialization. Returns an error if the key is missing or doesn't meet security requirements.
func NewManagerWithKey ¶
NewManagerWithKey creates a new encryption manager with a provided key. This is primarily used for testing. In production, use NewManager() with environment variables.