Core\Crypto::generateKey
Core\Crypto::generateKey(): secret bytesDraws a fresh key for Core\Crypto::seal and Core\Crypto::open.
A key is 32 bytes of random data. There is no key size to choose, because both ciphers Novis offers use one size. That is why this member takes no arguments. Every call gives a new key.
The type of the key is secret bytes. The secret part belongs to the type, so the program cannot print the key, write it to a log, or put it in an error message. It can pass the key to seal and open, and it can compare two keys with ==.
To store a key or send it somewhere, you write one Core\Secret::revealBytes call. That call needs a reason in text, so every place where a key leaves the program is easy to find.
Good to know: this is the member for a key nobody has to remember. For a key made from a password that a person types, use Core\Crypto::deriveKey.
Return value
Returns secret bytes. 32 bytes as secret bytes. secret is part of the type, so you cannot assign the key to a plain bytes. You also cannot print it with echo, log it or put it in a Throwable message.
Examples
Seal a note under a fresh key
<?nvs// Shows how to draw a key and seal a short note with it.
// `Core\Crypto::generateKey` takes no arguments and gives 32 random bytes.secret bytes $key = Core\Crypto::generateKey();
bytes $sealed = Core\Crypto::seal( 'the meeting moved to Thursday' as bytes, $key, Core\Crypto::Cipher::XChaCha20Poly1305,);
// The same key opens the note again. This prints the note.echo Core\Crypto::open($sealed, $key, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";the meeting moved to ThursdayEvery call gives a new key
<?nvs// Shows that two keys are never the same, and that the key which sealed a// message is the only one that opens it.
secret bytes $first = Core\Crypto::generateKey();secret bytes $second = Core\Crypto::generateKey();
// Two keys can be compared with `==` and `!=`. This prints `different`.echo ($first == $second ? 'the same' : 'different'), "\n";
bytes $sealed = Core\Crypto::seal( 'card ending 4242' as bytes, $first, Core\Crypto::Cipher::Aes256Gcm,);
// The second key throws a `RuntimeError` here. Keep the key that sealed the// data. Without it the data stays closed.try { Core\Crypto::open($sealed, $second, Core\Crypto::Cipher::Aes256Gcm); echo 'the other key opened it', "\n";} catch (RuntimeError $e) { echo 'the other key does not open it', "\n";}differentthe other key does not open itA key for every record
<?nvs// Shows how a program gives every stored record its own key. Each record key is// sealed under one main key and stored next to the record. Only the main key// has to be kept safe, and a key that leaks opens one record.
secret bytes $mainKey = Core\Crypto::generateKey();
array<string> $records = ['Ada: 1 box of pencils', 'Grace: 2 notebooks'];array<bytes> $sealedRecords = [];array<bytes> $sealedKeys = [];
foreach ($records as string $record) { secret bytes $recordKey = Core\Crypto::generateKey(); $sealedRecords[] = Core\Crypto::seal( $record as bytes, $recordKey, Core\Crypto::Cipher::XChaCha20Poly1305, );
// `Core\Secret::revealBytes` is the one way to pass a key on as data. It // takes a reason in text, which stays in the code for the next reader. $sealedKeys[] = Core\Crypto::seal( Core\Secret::revealBytes($recordKey, 'the record key is stored sealed under the main key'), $mainKey, Core\Crypto::Cipher::XChaCha20Poly1305, );}
// Reading a record back takes two steps. Open its key with the main key, then// open the record with that key. This prints both records.uint $i = 0;while ($i < 2) { bytes $openedKey = Core\Crypto::open( $sealedKeys[$i], $mainKey, Core\Crypto::Cipher::XChaCha20Poly1305, ); echo Core\Crypto::open( $sealedRecords[$i], $openedKey, Core\Crypto::Cipher::XChaCha20Poly1305, ) as string, "\n"; $i = $i + 1;}Ada: 1 box of pencilsGrace: 2 notebooks