Skip to content

Core\Crypto\KeyPair::write

$keyPair->write(): secret bytes

Gives you this pair's private key as bytes, so your program can store it and read it back later.

The result is secret bytes. Novis keeps a secret value out of your output, your logs and your error messages. To get plain bytes from it, call Core\Secret::revealBytes and give a reason. That reason stays in your code for the next person who reads it.

The bytes are PKCS#8, in the binary DER form. Core\Crypto\KeyPair::read takes them back, and so does any other program that reads PKCS#8. A pair that was read from a PEM block writes the binary form here, because that form is what the PEM block carried.

Good to know: a private key that leaves your program is a key somebody else can use. Store it where only your server can read it, and never send it to a client.

Return value

Returns secret bytes. The PKCS#8 as DER. It is DER even when the pair was read from PEM.

Examples

Store a key and read it back

<?nvs
// Shows what this method gives you, and how a key survives a restart. The pair
// is a new one, so no key from your machine is printed here.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
// `write` gives the private key as `secret bytes`.
secret bytes $key = $pair->write();
// `Core\Secret::revealBytes` takes a secret and a reason, and gives you plain
// bytes. This is the value your program stores.
bytes $plain = Core\Secret::revealBytes(
$key,
'the key is written to the file only this server can read',
);
echo 'the stored key is ', Core\Bytes::length($plain), ' bytes', "\n";
// The next start reads those bytes. It is the same pair, so it has the same
// public half and it signs the same way.
Core\Crypto\KeyPair $again = Core\Crypto\KeyPair::read($plain, Core\Crypto\KeyKind::Ed25519);
bytes $before = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
bytes $after = $again->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
echo 'the same key came back: ', ($before == $after ? 'yes' : 'no'), "\n";
Output
the stored key is 48 bytes
the same key came back: yes

The binary form whichever way the key arrived

<?nvs
// Shows that this method always gives the binary form. The pair below was read
// from a PEM block, which is text.
string $pem = "-----BEGIN PRIVATE KEY-----\n"
. "MC4CAQAwBQYDK2VwBCIEIDbnkTx1/12Juk9GUmMg9UfPdTaaVgoWj2q1uxePvqrf\n"
. "-----END PRIVATE KEY-----\n";
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read(
$pem as bytes,
Core\Crypto\KeyKind::Ed25519,
);
bytes $written = Core\Secret::revealBytes(
$pair->write(),
'the example prints a key that is written in its own source',
);
// The PEM block carried these octets as base64 between its two marker lines.
echo Core\Encoding::toHex($written), "\n";
// The binary form is smaller than the text, and reads back the same way.
echo Core\Str::length($pem), ' characters of text, ',
Core\Bytes::length($written), ' bytes of key', "\n";
Output
302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf
119 characters of text, 48 bytes of key

Hand the key to a tool that wants a pem file

<?nvs
// Shows how to write a key as a PEM file, which is the form many other tools
// read. The key comes out of your own storage first.
string $stored = '302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf';
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex($stored),
Core\Crypto\KeyKind::Ed25519,
);
bytes $der = Core\Secret::revealBytes(
$pair->write(),
'the key is exported to a file for the tool that needs it',
);
// A PEM file is those bytes as base64, between two marker lines.
string $pem = "-----BEGIN PRIVATE KEY-----\n"
. Core\Encoding::toBase64($der)
. "\n-----END PRIVATE KEY-----\n";
echo $pem;
Output
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIDbnkTx1/12Juk9GUmMg9UfPdTaaVgoWj2q1uxePvqrf
-----END PRIVATE KEY-----