Both directions
Both sides hold their own key pair in memory. Whoever sends seals for the public key of the other side, and only that side can open it.
| Server | Browser | |
|---|---|---|
| Encrypt | pqseal.Seal(key, context, data) | seal.encrypt(context, data) |
| As JSON | pqseal.SealJSON(key, context, value) | seal.encryptJSON(context, value) |
| Decrypt | sealer.Open(sealed, context) | seal.decrypt(context, sealed) |
| As text | string(data) | seal.decryptText(context, sealed) |
| As JSON | sealer.OpenJSON(sealed, context, &value) | seal.decryptJSON(context, sealed) |
| Own key | sealer.PublicKey() | seal.publicKey() |
Browser to server
Section titled “Browser to server”const sealed = await seal.encryptJSON('access/new', { host, user, sshKey });await fetch('/api/access', { method: 'POST', body: sealed });var access Accessif err := sealer.OpenJSON(string(c.Body()), "access/new", &access); err != nil { return pqfiber.Error(c, err)}Server to browser
Section titled “Server to browser”The browser sends its public key along. The way does not matter, it only has to reach the server.
const response = await fetch('/api/vault/7', { headers: { 'X-Client-Key': await seal.publicKey() },});const entry = await seal.decryptJSON('vault/7', await response.text());sealed, err := pqseal.SealJSON(c.Get("X-Client-Key"), "vault/7", entry)if err != nil { return pqfiber.Error(c, err)}return c.SendString(sealed)The context
Section titled “The context”context is a free, non-empty string. It is not transmitted and must be the same on both sides. It binds a message to its purpose: what was sealed for password-old does not open as password-new.
A good choice is the operation and the field name, for data of a single user also their ID, for example vault/7/password.
New keys
Section titled “New keys”Keys rotate, and a restart removes them. The server then answers with ErrUnknownKey. withFreshKey fetches the new key in that case and calls your function once more:
const response = await seal.withFreshKey(async () => fetch('/api/access', { method: 'POST', body: await seal.encrypt('access', data) }));Use this for every send. It needs an answer with status 409 and {"error":"unknown_key"}, as pqfiber.Error gives. Without HTTP, say over a WebSocket, call seal.refreshServerKey() yourself after unknown_key and send again.
Plain text allowed?
Section titled “Plain text allowed?”Whether a route also accepts plain text, say from API key clients, is up to the application. pqseal.IsSealed(value) helps, you decide. The library has no fallback.