crypto

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: BSD-3-Clause Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// KeySize is the AES-256 key size used by Encryptor.
	KeySize = 32
)

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(ciphertext string) (string, error)

Decrypt decrypts with the package default encryptor.

func Encrypt

func Encrypt(plaintext string) (string, error)

Encrypt encrypts with the package default encryptor.

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey creates a random AES-256 key.

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

func LoadKeyFile(path string) ([]byte, error)

LoadKeyFile reads a hex-encoded key file.

func ParseKey

func ParseKey(in string) ([]byte, error)

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

func SaveKeyFile(path string, key []byte) error

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 Default

func Default() (*Encryptor, error)

Default returns the package default encryptor.

func New

func New(keys Keys) (*Encryptor, error)

New creates an Encryptor from keys. Every key must be KeySize bytes.

func (*Encryptor) Decrypt

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

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.

func (*Encryptor) Encrypt

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

Encrypt encrypts a plaintext string. The empty string is returned unchanged so existing nullable stored-credential behavior remains explicit. Each call uses a fresh random 96-bit nonce, so a key must not seal more than 2^32 messages.

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

type RandomError struct {
	Err error
	Op  string
}

RandomError reports failure from the cryptographic random source.

func (*RandomError) Error

func (e *RandomError) Error() string

func (*RandomError) Unwrap

func (e *RandomError) Unwrap() error

Jump to

Keyboard shortcuts

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