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
- Create a seal keyset once and store the private JSON in a secret manager.
- Give senders the public keyset. Services that only encrypt never hold a secret.
- Seal on write and store the
pql1.text in any TEXT or JSON column. - 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 truncatedkeys, 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
- Confidentiality against an attacker who records the envelope today and has a quantum computer later.
- No plaintext from any modified envelope: a flipped bit, a truncation, a spliced header or a rewritten suite id all fail.
- No oracle: every failure is the same error, and the commitment is checked in constant time before decryption.
04When to use something else
| Situation | Use |
|---|---|
| Many fields in one row or table | seal_batch, one data key per batch |
| Files and backups larger than a few MB | stream, 64 KiB chunks |
| Data in transit between services | Hybrid TLS |
| Proving who wrote something | sign; 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/