Files and streams
stream seals files, backups and uploads of any size in 64 KiB chunks, without holding the whole file in memory.
01Seal and open a stream
import fs from 'node:fs';
import { stream } from 'pqlock';
const out = fs.createWriteStream('backup.tar.pql');
for await (const chunk of stream.sealStream(pub, fs.createReadStream('backup.tar'))) out.write(chunk);
out.end();
for await (const chunk of stream.openStream(keys, fs.createReadStream('backup.tar.pql'))) restore.write(chunk);from pqlock import stream
with open("backup.tar", "rb") as src, open("backup.tar.pql", "wb") as dst:
stream.seal_stream(src, dst, pub)
with open("backup.tar.pql", "rb") as src, open("backup.tar", "wb") as dst:
stream.open_stream(src, dst, keys)src, _ := os.Open("backup.tar")
dst, _ := os.Create("backup.tar.pql")
n, err := stream.SealStream(pub, dst, src)
// or wrap a writer: w, err := stream.NewWriter(pub, dst)02How it works
A sealed stream starts with a 1,271-byte data-key record (the data key sealed to the keyset), then 64 KiB chunks, each encrypted with AES-256-GCM and numbered so chunks cannot be reordered, dropped or truncated without detection. The last chunk is marked final, so a stream cut short fails to open.
Chunks are released as they verify, before the end of the stream is reached. If the consumer must never act on a partial file, write to a temporary location and commit only when open completes without error.
03Rotation
Like seal_batch, only the leading record is re-wrapped when the key rotates, so terabytes of backups do not need re-encrypting. See Key rotation.
pqlock v0.1.0 · unaudited preview · source of truth: spec/