crypto

package
v1.21.30 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package crypto provides encryption utilities for TMI.

Index

Constants

View Source
const MaxEncryptedValueLength = 4000

MaxEncryptedValueLength is the maximum length of an encrypted value string that fits in a varchar(4000) column.

Variables

View Source
var ErrValueTooLong = errors.New("encrypted value exceeds maximum storage length")

ErrValueTooLong is returned when an encrypted value exceeds the database column limit.

Functions

func ContextIDOf

func ContextIDOf(value string) (int, bool)

ContextIDOf parses the key id out of an ENC:v1 envelope.

func GenerateHMACSignature

func GenerateHMACSignature(payload []byte, secret string) string

GenerateHMACSignature generates an HMAC-SHA256 signature for the payload. Returns the signature in the format "sha256=<hex-encoded-mac>".

func IsEncrypted

func IsEncrypted(value string) bool

IsEncrypted returns true if the value has the ENC: prefix indicating encryption.

func VerifyHMACSignature

func VerifyHMACSignature(payload []byte, signature string, secret string) bool

VerifyHMACSignature verifies an HMAC-SHA256 signature using constant-time comparison.

Types

type EncryptionContext

type EncryptionContext struct {
	ContextID int
	Algorithm string
}

EncryptionContext tracks key version and cipher metadata.

type SettingsEncryptor

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

SettingsEncryptor encrypts and decrypts setting values using AES-256-GCM.

func NewSettingsEncryptor

func NewSettingsEncryptor(ctx context.Context, provider secrets.Provider) (*SettingsEncryptor, error)

NewSettingsEncryptor creates a new encryptor using the secrets provider. If no encryption key is found, returns a disabled encryptor that passes values through.

func NewSettingsEncryptorFromKeyring

func NewSettingsEncryptorFromKeyring(current []byte, currentID int, previous []byte, previousID int) (*SettingsEncryptor, error)

NewSettingsEncryptorFromKeyring builds an encryptor from raw keys with explicit ids. previousID 0 means "unknown" (selected by trial decrypt only).

func NewSettingsEncryptorFromKeys

func NewSettingsEncryptorFromKeys(currentKey, previousKey []byte, contextID int) (*SettingsEncryptor, error)

NewSettingsEncryptorFromKeys creates an encryptor directly from key bytes. This is primarily for testing. contextID defaults to 1.

func (*SettingsEncryptor) CurrentPrefix

func (e *SettingsEncryptor) CurrentPrefix() string

CurrentPrefix returns the envelope prefix values written by this encryptor carry.

func (*SettingsEncryptor) Decrypt

func (e *SettingsEncryptor) Decrypt(value string) (string, error)

Decrypt decrypts an encrypted value. If the value doesn't have the ENC: prefix, it is returned as-is (plaintext passthrough). Tries the current key first, then the previous key if configured and the current key fails.

func (*SettingsEncryptor) Encrypt

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

Encrypt encrypts a plaintext value using AES-256-GCM with the current key. Returns the original value unchanged if encryption is disabled.

func (*SettingsEncryptor) GetContext

func (e *SettingsEncryptor) GetContext() EncryptionContext

GetContext returns the current encryption context.

func (*SettingsEncryptor) HasPreviousKey

func (e *SettingsEncryptor) HasPreviousKey() bool

HasPreviousKey returns true if a previous key is configured for key rotation.

func (*SettingsEncryptor) IsEnabled

func (e *SettingsEncryptor) IsEnabled() bool

IsEnabled returns true if encryption is configured and active.

Jump to

Keyboard shortcuts

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