Core\Crypto::seal
Core\Crypto::seal(
bytes $message,
secret bytes $key,
Core\Crypto::Cipher $cipher,
): bytesEncrypts a message and protects it from being changed, in one step.
You give Core\Crypto::seal three things: the message, a 32 byte key, and the cipher to use. There is no mode, no padding and no IV to choose. The member draws a new nonce for every call and puts it in front of the result. Sealing the same message twice therefore gives two different results, and both of them open to the same message.
The result is 40 bytes longer than the message under XChaCha20Poly1305, and 28 bytes longer under Aes256Gcm. Use XChaCha20Poly1305 when a Novis program opens the message again. Use Aes256Gcm when a browser has to read it.
Core\Crypto::open is the other half. It throws an error when one byte of the sealed message has been changed, so nobody can alter what you sealed.
Parameters
$messagebytesThe plaintext. Asecretvalue is not allowed. To seal one, callCore\Secret::revealBytesfirst. Then a text search of your code finds every such place.$keysecret bytesA 32-byte key, such asgenerateKeyreturns. Both ciphers use a key of this size.$cipherCore\Crypto::CipherThe cipher to use. There is no default. UseXChaCha20Poly1305when both ends are Novis, andAes256Gcmwhen a browser must read the result.
Return value
Returns bytes. The sealed message. It is 40 bytes longer than $message with XChaCha20Poly1305, and 28 bytes longer with Aes256Gcm. It is different on every call for the same inputs, because each call uses its own nonce.
Errors
LogicError$keyis not 32 bytes long, so it is not a key.RuntimeErrorThis process does not have enough memory for a buffer the size of the sealed message.
Examples
The same message twice
<?nvs// Shows that sealing one message twice under one key gives two different// results. Both of them open to the same message.
secret bytes $key = Core\Crypto::generateKey();bytes $message = 'the meeting moved to Thursday' as bytes;
bytes $first = Core\Crypto::seal($message, $key, Core\Crypto::Cipher::XChaCha20Poly1305);bytes $second = Core\Crypto::seal($message, $key, Core\Crypto::Cipher::XChaCha20Poly1305);
// Each call draws its own nonce, so the two results differ. An attacker who// sees both cannot tell that the same message was sealed twice.echo ($first == $second ? 'the same' : 'different'), "\n";
// Both open to the message that was sealed. This prints the note twice.echo Core\Crypto::open($first, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";echo Core\Crypto::open($second, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";differentthe meeting moved to Thursdaythe meeting moved to ThursdayChoose the cipher
<?nvs// Shows the two ciphers and what each one costs in bytes. Pick// `XChaCha20Poly1305` when a Novis program opens the message. Pick `Aes256Gcm`// when a browser has to read it.
secret bytes $key = Core\Crypto::generateKey();bytes $message = 'the meeting moved to Thursday' as bytes;
// The message is 29 bytes long. This prints 29.echo Core\Bytes::length($message), "\n";
// `XChaCha20Poly1305` adds 40 bytes: a nonce of 24 and a tag of 16. This// prints 69.bytes $sealed = Core\Crypto::seal($message, $key, Core\Crypto::Cipher::XChaCha20Poly1305);echo Core\Bytes::length($sealed), "\n";
// `Aes256Gcm` adds 28 bytes: a nonce of 12 and a tag of 16. This prints 57.bytes $forTheBrowser = Core\Crypto::seal($message, $key, Core\Crypto::Cipher::Aes256Gcm);echo Core\Bytes::length($forTheBrowser), "\n";
// The cipher used to open a message must be the one that sealed it.echo Core\Crypto::open($forTheBrowser, $key, Core\Crypto::Cipher::Aes256Gcm) as string, "\n";296957the meeting moved to ThursdayStore a phone number in a row
<?nvs// Shows how a program seals one field of a record before it is stored. The// column holds text, so the sealed bytes are written as hex. Somebody who// reads the table sees the hex and nothing else.
secret bytes $key = Core\Crypto::generateKey();
array<string> $customers = ['Ada', 'Grace'];array<string> $numbers = ['+44 7700 900123', '+1 202 555 0147'];array<string> $column = [];
uint $i = 0;while ($i < 2) { bytes $sealed = Core\Crypto::seal( $numbers[$i] as bytes, $key, Core\Crypto::Cipher::XChaCha20Poly1305, ); $column[] = Core\Encoding::toHex($sealed); $i = $i + 1;}
// What the row holds is text, and it is 110 characters long for a number of// 15. This prints 110.echo Core\Str::length($column[0]), "\n";
// The program reads the row back and opens the field. This prints both names// with their numbers.$i = 0;while ($i < 2) { bytes $stored = Core\Encoding::fromHex($column[$i]); echo $customers[$i], ': ', Core\Crypto::open($stored, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n"; $i = $i + 1;}110Ada: +44 7700 900123Grace: +1 202 555 0147