pii

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 10 Imported by: 3 modules, 2 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

Overview

Package pii is the shared application-level PII primitive: AES-256-GCM field encryption with deterministic HMAC blind indexes for exact-match lookups. Randomized GCM ciphertext cannot be queried, so each lookup carries one HMAC column over the normalized lookup value.

The envelope doubles as the backfill detector and the future rotation marker: encrypted values carry the v1: prefix, legacy plaintext rows do not. Decrypt passes prefix-less values through untouched, so rows written before encryption read back byte-identical. Empty strings stay empty and are never encrypted — they carry no information to protect.

A value that literally begins with the prefix in a legacy row is indistinguishable from ciphertext; operators must re-key such rows through the encrypted write path before enabling the key.

Index

Constants

View Source
const (
	// Prefix marks an encrypted envelope. It doubles as the backfill
	// detector: rows without it are legacy plaintext.
	Prefix = "v1:"
	// NonceSize is the GCM nonce width in bytes.
	NonceSize = 12
	// MasterKeySize is the required master-key width in bytes (AES-256).
	MasterKeySize = 32
)

Variables

View Source
var (
	// ErrInvalidMasterKey is returned when the master key is not exactly
	// MasterKeySize bytes.
	ErrInvalidMasterKey = errors.New("pii: master key must be 32 bytes")
	// ErrCorruptCiphertext is returned when an enveloped value fails to
	// decode or open. Callers surface it as a store error — never silent
	// wrong data.
	ErrCorruptCiphertext = errors.New("pii: ciphertext is corrupt")
)

Functions

func IsEncrypted

func IsEncrypted(stored string) bool

IsEncrypted reports whether the stored value carries the encrypted envelope. Prefix-less values are legacy plaintext.

Types

type Cipher

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

Cipher encrypts single PII fields and derives their blind indexes. One instance serves every commerce store; it holds only subkeys, never the master key, and is safe for concurrent use.

func New

func New(master []byte) (*Cipher, error)

New builds the cipher over the 32-byte master key, deriving separate encryption and blind-index subkeys via HKDF-SHA256. Any other length fails with ErrInvalidMasterKey.

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(stored string) (string, error)

Decrypt opens one stored PII field. Values without the v1: prefix are legacy plaintext and pass through untouched; empty stays empty. A corrupt envelope reports ErrCorruptCiphertext — never silent wrong data.

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(plaintext string) (string, error)

Encrypt seals one PII field into the v1: envelope. Empty input stays empty and is never encrypted.

func (*Cipher) Index

func (c *Cipher) Index(normalized string) string

Index derives the deterministic blind index over the normalized lookup value: the same input always yields the same index, and distinct inputs (barring a hash collision) yield distinct indexes. Callers normalize before indexing — the store indexes the already-normalized email the service passes, so the write-time and lookup-time forms must match. Empty input stays empty.

Jump to

Keyboard shortcuts

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