Database fields
seal_batch encrypts many fields under one data key and binds each field to its table, column and row, so a ciphertext copied into another row does not open.
01Why batch
A full PQL1 envelope adds 1,195 bytes. For a table with millions of small fields that adds up. seal_batch wraps one data key in a single post-quantum record and encrypts each field with AES-256-GCM, adding 29 bytes per field.
02Seal a batch
import { keyset, sealBatch } from 'pqlock';
const items = [
{ context: { table: 'customers', column: 'card', row: '42' }, plaintext: Buffer.from('4242 4242 4242 4242') },
{ context: { table: 'customers', column: 'ssn', row: '42' }, plaintext: Buffer.from('078-05-1120') },
];
const { record, fields } = sealBatch.sealBatch(keys, items);
// store record once (1,271 bytes), and fields[i] in its column
const plain = sealBatch.openBatch(keys, record, items.map(i => i.context), fields);from pqlock import seal_batch
items = [
(("customers", "card", "42"), b"4242 4242 4242 4242"),
(("customers", "ssn", "42"), b"078-05-1120"),
]
record, fields = seal_batch.seal_batch(items, keys)
plain = seal_batch.open_batch(record, [c for c, _ in items], fields, keys)items := []sealbatch.Item{
{Context: sealbatch.Context{Table: "customers", Column: "card", Row: "42"}, Plaintext: []byte("4242 4242 4242 4242")},
}
record, fields, err := sealbatch.SealBatch(keys, items)
plain, err := sealbatch.OpenBatch(keys, record, contexts, fields)03Context binding
Each field's associated data includes the batch id and its (table, column, row). Opening a field with any other context fails, which stops an attacker with database write access from swapping ciphertexts between rows. Table, column and row must all be non-empty.
04Rotating the key
The record holds the data key sealed to the keyset. Rotation re-wraps only that record; the fields never change. See Key rotation.
The repository's uc2-db-fields use case runs a Go service against a real PostgreSQL database on every change.
pqlock v0.1.0 · unaudited preview · source of truth: spec/