Skip to content

Core\Crypto\PublicKey::Format

Says which encoding a public key is read from or written to.

Core\Crypto\PublicKey::Format is declared in the class Core\Crypto\PublicKey. The methods read and write take it as an argument.

A public key can be written as bytes in several standard ways. Core\Crypto\PublicKey::Format has one case for each encoding that a browser's WebCrypto API exports, and the cases have the same names. Raw is the key bytes alone. Spki is a DER document that contains the key and the name of its algorithm. Jwk is the JSON form that identity providers publish. When you read a key, you pass the encoding the bytes are in. When you write a key, you pass the encoding the receiver needs.

Good to know: Raw does not exist for RSA keys, and reading an RSA key as Raw throws a LogicError. Spki works with every kind of key. Writing a key always gives the same bytes for the same key and encoding. It does not matter which encoding the key was read from.

The examples below write one key in all three encodings, read a key in one encoding and write it in another, and publish a list of keys as JSON.

Examples

One key written three ways

<?nvs
// This program writes one public key in each of the three key formats. The key
// is the same each time. Only the data around it changes.
// `PublicKey::read` reads a P-256 key in the `Spki` format, as DER bytes.
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex(
'3059301306072a8648ce3d020106082a8648ce3d030107034200'
. '0468aa7eb5d775b8ab32e53b3a2b006dc42638d4d5a06d9b50c49'
. '3781e2a65f3e036cb273ea410534a6e8c946ae9ea7e10318686fa'
. 'eeba6850b16fdb2572ff0e89',
),
Core\Crypto\KeyKind::P256,
Core\Crypto\PublicKey::Format::Spki,
);
// `Raw` writes the point itself. The first byte says that the point is
// uncompressed, and the two coordinates follow it.
echo 'raw ', Core\Encoding::toHex($key->write(Core\Crypto\PublicKey::Format::Raw)), "\n";
// `Spki` writes the point with the name of its algorithm in front. Every kind
// of key can be written in this format.
echo 'spki ', Core\Bytes::length($key->write(Core\Crypto\PublicKey::Format::Spki)), ' octets', "\n";
// `Jwk` writes the key as JSON, the format for keys published on the web. It
// has only the fields that identify the key, sorted by name.
echo 'jwk ', Core\Encoding::decodeText(
$key->write(Core\Crypto\PublicKey::Format::Jwk),
Core\Charset::Utf8,
), "\n";
Output
raw 0468aa7eb5d775b8ab32e53b3a2b006dc42638d4d5a06d9b50c493781e2a65f3e036cb273ea410534a6e8c946ae9ea7e10318686faeeba6850b16fdb2572ff0e89
spki 91 octets
jwk {"crv":"P-256","kty":"EC","x":"aKp-tdd1uKsy5Ts6KwBtxCY41NWgbZtQxJN4Hipl8-A","y":"NssnPqQQU0pujJRq6ep-EDGGhvruumhQsW_bJXL_Dok"}

In on one encoding out on another

<?nvs
// A key can be read in one format and written in another. Each format is written
// from the key itself, so the key stays the same. This program reads DER, writes
// the raw point and JSON, and reads both back.
string $der = '3059301306072a8648ce3d020106082a8648ce3d030107034200'
. '0468aa7eb5d775b8ab32e53b3a2b006dc42638d4d5a06d9b50c49'
. '3781e2a65f3e036cb273ea410534a6e8c946ae9ea7e10318686fa'
. 'eeba6850b16fdb2572ff0e89';
Core\Crypto\PublicKey $stored = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex($der),
Core\Crypto\KeyKind::P256,
Core\Crypto\PublicKey::Format::Spki,
);
// The browser gets the raw point and sends it back later. `PublicKey::read`
// needs the format of the bytes as an argument.
bytes $forTheBrowser = $stored->write(Core\Crypto\PublicKey::Format::Raw);
Core\Crypto\PublicKey $returned = Core\Crypto\PublicKey::read(
$forTheBrowser,
Core\Crypto\KeyKind::P256,
Core\Crypto\PublicKey::Format::Raw,
);
if (Core\Encoding::toHex($returned->write(Core\Crypto\PublicKey::Format::Spki)) == $der) {
echo 'the raw point wrote back the DER we started from', "\n";
} else {
echo 'the raw point came back as another key', "\n";
}
// The same test with JSON, the format for keys published on the web.
Core\Crypto\PublicKey $viaJson = Core\Crypto\PublicKey::read(
$stored->write(Core\Crypto\PublicKey::Format::Jwk),
Core\Crypto\KeyKind::P256,
Core\Crypto\PublicKey::Format::Jwk,
);
if (Core\Encoding::toHex($viaJson->write(Core\Crypto\PublicKey::Format::Spki)) == $der) {
echo 'and so did the JSON one', "\n";
} else {
echo 'the JSON one came back as another key', "\n";
}
Output
the raw point wrote back the DER we started from
and so did the JSON one

Publishing the keys clients verify with

<?nvs
// This program publishes the public keys of a service as JSON. Clients use
// them to verify signatures. The keys are stored as DER, and the program
// writes the JSON from them.
array<string> $stored = [
'3059301306072a8648ce3d020106082a8648ce3d030107034200'
. '0468aa7eb5d775b8ab32e53b3a2b006dc42638d4d5a06d9b50c49'
. '3781e2a65f3e036cb273ea410534a6e8c946ae9ea7e10318686fa'
. 'eeba6850b16fdb2572ff0e89',
'302a300506032b6570032100fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',
];
array<Core\Crypto\KeyKind> $kinds = [
Core\Crypto\KeyKind::P256,
Core\Crypto\KeyKind::Ed25519,
];
array<string> $published = [];
foreach (Core\Arr::range(0, 1) as int $slot) {
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex($stored[$slot]),
$kinds[$slot],
Core\Crypto\PublicKey::Format::Spki,
);
$published[] = Core\Encoding::decodeText(
$key->write(Core\Crypto\PublicKey::Format::Jwk),
Core\Charset::Utf8,
);
}
// `write` with `Jwk` gives only the fields that identify the key, sorted by
// name. A real service can add its own `kid` and `alg` fields here.
echo "{\"keys\":[", Core\Str::join($published, ','), ']}', "\n";
Output
{"keys":[{"crv":"P-256","kty":"EC","x":"aKp-tdd1uKsy5Ts6KwBtxCY41NWgbZtQxJN4Hipl8-A","y":"NssnPqQQU0pujJRq6ep-EDGGhvruumhQsW_bJXL_Dok"},{"crv":"Ed25519","kty":"OKP","x":"_M6QCKM1ZSSFYSlafOHMywQLYSm7qP8kdElTu8ASYaw"}]}