PluginCrypto
Locks and unlocks text, and signs it. Using this object requires the crypto permission. Locking text for other devices and for friends, and signing, additionally require the crypto:friends permission.
There are three ways to lock text. The first uses a password: deriveKey turns the password into a key, and the key locks and unlocks text with seal and open. The second uses a random key: newKey creates one, keyOf turns it into a key that locks and unlocks text, and split cuts it in parts that are all needed to join it again with join. The third uses the keys of the devices: sealTo locks a text for the people that the plugin names, and the app handles all the keys, so the plugin never receives one.
Examples
Section titled “Examples”Lock a text with a password, and unlock it again later
// Made once, and saved next to the locked text.const params = ctx.crypto.newParams();
const key = await ctx.crypto.deriveKey(password, params);const locked = key.seal('my secret', 'row:' + row.id);
// After a restart, the same password and the same settings give the same key.const sameKey = await ctx.crypto.deriveKey(password, params);const text = sameKey.open(locked, 'row:' + row.id); // 'my secret'Lock a text with a random key, cut in three parts that are all needed
const key = ctx.crypto.newKey();const locked = ctx.crypto.keyOf(key).seal('the letter', 'row:' + row.id);const parts = ctx.crypto.split(key, 3);
// Later, with the three parts, in any order.const text = ctx.crypto.keyOf(ctx.crypto.join(parts)).open(locked, 'row:' + row.id); // 'the letter'Lock a text for all the devices of the person, and read it
const envelope = await ctx.crypto.sealTo({ me: true }, 'dinner at eight', { purpose: 'note' });const opened = await ctx.crypto.open(envelope, { purpose: 'note' });if (opened) ctx.log('info', opened.text);Sign a text, and check the signature
const signature = await ctx.crypto.sign('I agree', { purpose: 'deal' });const who = await ctx.crypto.verify(signature, 'I agree', { purpose: 'deal' }); // { me: true }Methods
Section titled “Methods”deriveKey()
Section titled “deriveKey()”deriveKey(password, params): Promise<SealingKey>;Turns a password into a key that can lock and unlock text. The same password and the same settings always give the same key.
The calculation takes about one second. This is intended: a person who tries to guess the password has to wait the same time for every guess. The app stays responsive while the calculation runs.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
password |
string |
the password that the person typed |
params |
VaultParams |
the settings that newParams created. The call throws an error when they ask for less work than the default, because someone may have lowered them |
Returns
Section titled “Returns”Promise<SealingKey>
a key whose seal method locks a text and whose open method unlocks it
Example
Section titled “Example”const key = await ctx.crypto.deriveKey(password, params);const locked = key.seal('my secret', 'row:' + row.id);join()
Section titled “join()”join(parts): string;Joins the parts that split made, in any order. With a part missing or changed, the result is another key, which opens nothing.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
parts |
readonly string[] |
all the parts |
Returns
Section titled “Returns”string
the key
keyOf()
Section titled “keyOf()”keyOf(key): SealingKey;Turns a key that newKey or join gave into a key that locks and unlocks text, in the same way as a key that deriveKey made from a password.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key |
string |
the key, as a text of 43 characters |
Returns
Section titled “Returns”a key whose seal method locks a text and whose open method unlocks it
Throws
Section titled “Throws”an error when the text is not a key
newKey()
Section titled “newKey()”newKey(): string;Creates a new random key: 32 random bytes, written as a text of 43 characters. keyOf turns it into a key that locks and unlocks text, without a password and without waiting.
The key is a secret: a plugin keeps it only where the people who may read the text can read it, for example cut in parts with split and each part locked for one person with sealTo.
Returns
Section titled “Returns”string
the key, as a text
Example
Section titled “Example”const key = ctx.crypto.newKey();const locked = ctx.crypto.keyOf(key).seal('my secret', 'row:' + row.id);newParams()
Section titled “newParams()”newParams(cost?): VaultParams;Creates the settings that deriveKey needs to turn a password into a key. The settings consist of a random value called a salt, which makes the same password produce a different key each time, and of a cost, which says how much work deriveKey has to do.
The settings are not secret, but the plugin must save them next to the text that the key protects, because the same password produces a different key when the settings are different.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
cost? |
{ N: number; p: number; r: number; } |
the amount of work, as { N, r, p }. It can be higher than the default, and it can never be lower. When it is omitted, the default is used |
cost.N |
number |
- |
cost.p |
number |
- |
cost.r |
number |
- |
Returns
Section titled “Returns”the settings, to pass to deriveKey
Example
Section titled “Example”const params = ctx.crypto.newParams();open()
Section titled “open()”open(envelope, options): Promise< | { from: Whose; text: string;}| null>;Opens an envelope that sealTo created, and tells who locked it. Requires crypto:friends.
The call returns null, and does not throw an error, when the envelope cannot be opened. This happens when the envelope is not for this device, when it was created by another plugin, when its purpose is different, when it was changed, and when it was locked by someone whom the person does not know.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
envelope |
Envelope |
the envelope that sealTo returned |
options |
{ purpose: string; } |
purpose must be the same word that was given to sealTo |
options.purpose |
string |
- |
Returns
Section titled “Returns”Promise<
| {
from: Whose;
text: string;
}
| null>
the text and the sender, or null. The sender is { me: true } for a device of the person, and { friend, name } for a friend, where friend is the id of the friend’s group
Example
Section titled “Example”const opened = await ctx.crypto.open(envelope, { purpose: 'note' });if (opened) { ctx.log('info', opened.text); if (!('me' in opened.from)) ctx.log('info', 'Locked by ' + opened.from.name);}sealTo()
Section titled “sealTo()”sealTo( to, text, options): Promise<Envelope>;Locks a text so that only the people that the plugin names can read it, and signs it with this device. Requires crypto:friends.
The result is an envelope, which is plain data: the plugin can save it, for example in a facet, or send it. An envelope can only be opened by the plugin that created it, and only with the same purpose.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
to |
To |
who can read the text: { me: true } for all the devices of the person, { device: id } for one device of the person, or { friend: groupId } for the devices of a friend, where groupId is the id of the friend’s group. The same id appears as from.friend in the result of open and verify. A key is never given |
text |
string |
the text to lock, at most 256 KiB |
options |
{ purpose: string; } |
purpose is a word that the plugin chooses to say what the text is for, for example note |
options.purpose |
string |
- |
Returns
Section titled “Returns”Promise<Envelope>
the envelope that contains the locked text
Throws
Section titled “Throws”an error when the friend is not a friend of the person, when the device is not in the group of the person, or when purpose is missing
Example
Section titled “Example”const envelope = await ctx.crypto.sealTo({ me: true }, 'dinner at eight', { purpose: 'note' });sign()
Section titled “sign()”sign(text, options): Promise<Signature>;Signs a text with this device. A signature shows that this device wrote the text, and that the text has not changed since. Requires crypto:friends.
The signature is valid only for this plugin and for the purpose that was given. It is plain data, so the plugin can save it or send it.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text |
string |
the text to sign |
options |
{ purpose: string; } |
purpose is a word that the plugin chooses to say what the signature is for, for example deal |
options.purpose |
string |
- |
Returns
Section titled “Returns”Promise<Signature>
the signature
Example
Section titled “Example”const signature = await ctx.crypto.sign('I agree', { purpose: 'deal' });split()
Section titled “split()”split(key, n): string[];Cuts a key in parts that are all needed to join it again. Any number of parts less than all of them says nothing about the key.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key |
string |
the key, from newKey |
n |
number |
the number of parts, from 2 to 32 |
Returns
Section titled “Returns”string[]
the parts, each a text of 43 characters
Example
Section titled “Example”// one part for each person: the text opens only when all of them give their part backconst parts = ctx.crypto.split(key, 3);verify()
Section titled “verify()”verify( signature, text, options): Promise<Whose | null>;Checks a signature against a text, and tells who signed it. Requires crypto:friends.
The call returns null when the signature does not belong to the text, when it was made for another plugin or another purpose, and when the signer is someone whom the person does not know.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
signature |
Signature |
the signature that sign returned |
text |
string |
the text that was signed |
options |
{ purpose: string; } |
purpose must be the same word that was given to sign |
options.purpose |
string |
- |
Returns
Section titled “Returns”Promise<Whose | null>
who signed the text, as { me: true } or { friend, name }, or null
Example
Section titled “Example”const who = await ctx.crypto.verify(signature, 'I agree', { purpose: 'deal' }); // { me: true }const forged = await ctx.crypto.verify(signature, 'I agree to pay', { purpose: 'deal' }); // null