Skip to content

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.

ServerBrowser
Encryptpqseal.Seal(key, context, data)seal.encrypt(context, data)
As JSONpqseal.SealJSON(key, context, value)seal.encryptJSON(context, value)
Decryptsealer.Open(sealed, context)seal.decrypt(context, sealed)
As textstring(data)seal.decryptText(context, sealed)
As JSONsealer.OpenJSON(sealed, context, &value)seal.decryptJSON(context, sealed)
Own keysealer.PublicKey()seal.publicKey()
const sealed = await seal.encryptJSON('access/new', { host, user, sshKey });
await fetch('/api/access', { method: 'POST', body: sealed });
var access Access
if err := sealer.OpenJSON(string(c.Body()), "access/new", &access); err != nil {
return pqfiber.Error(c, err)
}

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)

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.

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.

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.