Skip to content

Core\Crypto\PublicKey::write

Writes a public key out as octets, in the encoding you name, so you can send it or store it.

The three encodings are the ones Core\Crypto\PublicKey::read accepts: the plain key material (Raw), a SubjectPublicKeyInfo document (Spki), and a JSON Web Key (Jwk). A browser reads all three, so a key crosses to one in whichever form the other end asked for.

The octets are written from the key itself, not copied out of the octets the key arrived in. One key therefore has one spelling per encoding, however it was read. A Jwk holds the members the standard requires, sorted by name, and nothing else. Members such as ext and key_ops are left out, even when the key was read from a document that had them.

Good to know: an RSA key has no Raw form, so asking for one is an error in your program. Spki and Jwk are the encodings it has.

Parameters

  • $formatCore\Crypto\PublicKey::FormatThe format to write. Jwk gives the fields that RFC 7638 requires, sorted. A thumbprint is computed over this form.

Return value

Returns bytes. The bytes of the key. For a JWK, this is its UTF-8 JSON.

Errors

  • LogicError$format is Raw and the key has an RSA kind. An RSA key has no raw form, so there is nothing to return.

Examples

Send your public key to the other end

<?nvs
// Shows how to write your public key out so you can send it to somebody else.
// `Spki` is the form a certificate and a `.pem` public key carry.
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex('302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf'),
Core\Crypto\KeyKind::Ed25519,
);
// `publicKey` gives you the half you may send. `write` turns it into octets in
// the encoding you name.
bytes $sent = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
// This prints the same line on every run, because the file holds one key.
echo Core\Encoding::toHex($sent), "\n";
Output
302a300506032b6570032100fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac

A jwk for a browser

<?nvs
// Shows how to write a public key as a JWK, which is the form a browser reads.
// A JWK is a small JSON document.
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex('fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac'),
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Raw,
);
// The document holds the members the standard requires, sorted by name, and
// nothing else. A key read from a longer JWK writes this same short one.
echo $key->write(Core\Crypto\PublicKey::Format::Jwk) as string, "\n";
Output
{"crv":"Ed25519","kty":"OKP","x":"_M6QCKM1ZSSFYSlafOHMywQLYSm7qP8kdElTu8ASYaw"}

The encoding the other end asked for

<?nvs
// Shows a server that hands out its public key in whichever encoding the
// client asked for. One key has one spelling per encoding, so every client
// gets the same key.
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex('302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf'),
Core\Crypto\KeyKind::Ed25519,
);
Core\Crypto\PublicKey $key = $pair->publicKey();
// What each client asked for, as the names the browsers use.
array<string> $asked = ['raw', 'spki', 'jwk'];
foreach ($asked as string $name) {
Core\Crypto\PublicKey::Format $format = Core\Crypto\PublicKey::Format::Jwk;
if ($name == 'raw') {
$format = Core\Crypto\PublicKey::Format::Raw;
}
if ($name == 'spki') {
$format = Core\Crypto\PublicKey::Format::Spki;
}
// The same key, written three ways. This prints 32, 44 and 79 octets.
echo $name, ': ', Core\Bytes::length($key->write($format)), ' octets', "\n";
}
Output
raw: 32 octets
spki: 44 octets
jwk: 79 octets