Documentation
¶
Index ¶
- Constants
- func Decrypt(ciphertext string) (string, error)
- func Encrypt(plaintext string) (string, error)
- func GenerateKey() ([]byte, error)
- func InitEncryption(cfg Config)
- func LoadKeyFile(path string) ([]byte, error)
- func ParseKey(in string) ([]byte, error)
- func SaveKeyFile(path string, key []byte) error
- type CiphertextError
- type Config
- type DecodeError
- type DecryptError
- type Encryptor
- type InvalidKeyError
- type Keys
- type RandomError
Constants ¶
const (
// KeySize is the AES-256 key size used by Encryptor.
KeySize = 32
)
Variables ¶
This section is empty.
Functions ¶
func InitEncryption ¶
func InitEncryption(cfg Config)
InitEncryption initializes the package default encryptor. Applications should prefer constructing an Encryptor with New and passing it explicitly. It panics without a key unless Environment is "development", where a public fixed key is used.
func LoadKeyFile ¶
LoadKeyFile reads a hex-encoded key file.
func ParseKey ¶
ParseKey parses explicit key material. Hex and base64 keys must use the "hex:" or "base64:" prefixes; unprefixed values are treated as passphrases and derived with HKDF-SHA256 to avoid ambiguous hex-looking raw secrets.
func SaveKeyFile ¶
SaveKeyFile atomically writes a key as hex with 0600 permissions.
Types ¶
type CiphertextError ¶
type CiphertextError struct {
Reason string
}
CiphertextError reports malformed ciphertext.
func (*CiphertextError) Error ¶
func (e *CiphertextError) Error() string
type Config ¶
type Config struct {
EncryptionKey string
// Environment must be "development" for an empty EncryptionKey to be accepted.
Environment string
}
Config contains the key material used to initialize the package default encryptor for applications that have not yet threaded an Encryptor through every callsite.
type DecodeError ¶
type DecodeError struct {
Err error
}
DecodeError reports ciphertext decoding failures.
func (*DecodeError) Error ¶
func (e *DecodeError) Error() string
func (*DecodeError) Unwrap ¶
func (e *DecodeError) Unwrap() error
type DecryptError ¶
type DecryptError struct {
Err error
}
DecryptError reports authenticated decryption failure.
func (*DecryptError) Error ¶
func (e *DecryptError) Error() string
func (*DecryptError) Unwrap ¶
func (e *DecryptError) Unwrap() error
type Encryptor ¶
type Encryptor struct {
// contains filtered or unexported fields
}
Encryptor encrypts and decrypts strings with AES-GCM.
func (*Encryptor) Decrypt ¶
Decrypt decrypts a base64 encoded AES-GCM ciphertext string. Old keys are tried after the primary key so callers can rotate keys without data loss.
type InvalidKeyError ¶
type InvalidKeyError struct {
Reason string
}
InvalidKeyError reports invalid encryption key material.
func (*InvalidKeyError) Error ¶
func (e *InvalidKeyError) Error() string
type Keys ¶
type Keys struct {
// Primary is the AES-256 key used to encrypt and tried first when decrypting.
Primary []byte
// Previous holds retired keys that are only used to decrypt during rotation.
Previous [][]byte
}
Keys is the key material for an Encryptor.
type RandomError ¶
RandomError reports failure from the cryptographic random source.
func (*RandomError) Error ¶
func (e *RandomError) Error() string
func (*RandomError) Unwrap ¶
func (e *RandomError) Unwrap() error