Docs / Guides

Encrypt data

Use seal for any secret up to a few megabytes: card numbers, API credentials, messages, documents. Senders need only the public keyset.

01The pattern

  1. Create a seal keyset once and store the private JSON in a secret manager.
  2. Give senders the public keyset. Services that only encrypt never hold a secret.
  3. Seal on write and store the pql1. text in any TEXT or JSON column.
  4. Open on read in the service that holds the private keyset, and treat the one error type as "not readable".

02Seal and open

import { keyset, seal, PQLOpenError } from 'pqlock';

const keys = keyset.load(process.env.PQL_SEAL_KEYSET!);   // private, from the secret store
const pub = keyset.toPublic(keys);

const text = seal.toText(seal.seal(pub, Buffer.from(cardNumber)));
await db.query('UPDATE customers SET card = $1 WHERE id = $2', [text, id]);

try {
  const card = seal.open(keys, row.card).toString();
} catch (err) {
  if (err instanceof PQLOpenError) { /* wrong key, tampered or truncated */ }
  throw err;
}
import os
from pqlock import keyset, seal
from pqlock.errors import OpenError

keys = keyset.load(os.environ["PQL_SEAL_KEYSET"])
pub = keyset.public(keys)

text = seal.to_text(seal.seal(card_number.encode(), pub))

try:
    card = seal.open(row["card"], keys).decode()
except OpenError:
    ...  # wrong key, tampered or truncated
keys, err := keyset.Load([]byte(os.Getenv("PQL_SEAL_KEYSET")))
pub := keyset.Public(keys)

text, err := seal.SealText(pub, []byte(cardNumber))

card, err := seal.OpenText(keys, row.Card)
if errors.Is(err, pql.ErrOpen) {
    // wrong key, tampered or truncated
}

03What is guaranteed

04When to use something else

SituationUse
Many fields in one row or tableseal_batch, one data key per batch
Files and backups larger than a few MBstream, 64 KiB chunks
Data in transit between servicesHybrid TLS
Proving who wrote somethingsign; sealing hides data but does not authenticate the sender
Sealing is not sender authentication

Anyone with the public keyset can seal. If the reader must know who sealed, sign the plaintext before sealing, or sign the envelope.

pqlock v0.1.0 · unaudited preview · source of truth: spec/