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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Encrypt seals one PII field into the v1: envelope. Empty input stays empty and is never encrypted.
func (*Cipher) Index ¶
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.