Skip to content

Core\Crypto\PublicKey::kind

$publicKey->kind(): Core\Crypto\KeyKind

Reports which kind of key this is, as a case of Core\Crypto\KeyKind.

The answer is the kind that was named when the key was read, and it never changes afterwards. That matters most for RSA. The octets of an RSA key are the same whether the key is meant for RsaPkcs1 or for RsaPss, so nothing in the material could tell you, and the program that was handed the key is the only one that can say.

Use it to decide what a key is for before you use it. An X25519 key agrees a shared secret with Core\Crypto::agree. The other four kinds check signatures with Core\Crypto::verify.

Good to know: the kind is kept beside the key, so asking costs nothing. A key that came from Core\Crypto\KeyPair::publicKey reports the kind of the pair it belongs to.

Return value

Returns Core\Crypto\KeyKind. The case the key was read as. For an RSA key, it is RsaPkcs1 or RsaPss. This is the scheme of the key, and it is set when the key is read.

Examples

Ask a key what it is

<?nvs
// Shows how to ask a public key what it is. A key knows its kind because you
// named the kind when you read it.
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex('fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac'),
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Raw,
);
// `kind` returns a case of `Core\Crypto\KeyKind`, so you compare it with the
// case you are looking for. This prints the first line.
if ($key->kind() == Core\Crypto\KeyKind::Ed25519) {
echo 'this key checks signatures', "\n";
} else {
echo 'this key is for something else', "\n";
}
Output
this key checks signatures

One rsa key two schemes

<?nvs
// Shows why you name the kind when you read an RSA key. The octets of an RSA
// key are the same for both schemes, so the kind is the one you named.
bytes $sameOctets = Core\Encoding::fromHex('30820122300d06092a864886f70d01010105000382010f003082010a0282010100cfaa61932d8fd9e7858fa521617621f05bcb3411e174995d1fab033995201d0d9d72e5e8e133d88e8bec27e75068fb7c715eb712cae5750396d50af9ad2b5c45f0a7ab16bcf2cde4566873349a2407fa9452913e433b1d7f1ea74bda50d3f5d0ec2b0b50a4ea3023a2ae3ad4975390cd3b000d8020a578469f4bffa3e9864eb989e67a804a2cf08be53bba1bb0e9071f24156b0383730779d42efbf1daada62835a8e5e1891845cc782c322f9dc7f9eb6ff1c628c925f26ba29978a96cd10b2f61f03f361250261f860927632b6167bcba71f2e4d8df93e142c904a2515cd06af13c856bde1872ee30f3a19d8292cd2ce4b39c9788a0d3c5e3f33fe784c4e0330203010001');
Core\Crypto\PublicKey $signing = Core\Crypto\PublicKey::read(
$sameOctets,
Core\Crypto\KeyKind::RsaPkcs1,
Core\Crypto\PublicKey::Format::Spki,
);
Core\Crypto\PublicKey $probabilistic = Core\Crypto\PublicKey::read(
$sameOctets,
Core\Crypto\KeyKind::RsaPss,
Core\Crypto\PublicKey::Format::Spki,
);
// Two keys out of one document, and each reports the scheme it was read as.
// This prints `RS256` and then `PS256`.
if ($signing->kind() == Core\Crypto\KeyKind::RsaPkcs1) {
echo 'RS256', "\n";
}
if ($probabilistic->kind() == Core\Crypto\KeyKind::RsaPss) {
echo 'PS256', "\n";
}
Output
RS256
PS256

Sort the keys your clients sent

<?nvs
// Shows a server sorting the keys its clients sent. Agreement and signatures
// are different parts of the program, and `kind` says which one a key belongs
// to.
array<Core\Crypto\PublicKey> $sent = [
Core\Crypto\PublicKey::read(
Core\Encoding::fromHex('9c7be3a9c961ce008340674cd3d468fc5be27c98b65a35262ca2d05573d0585e'),
Core\Crypto\KeyKind::X25519,
Core\Crypto\PublicKey::Format::Raw,
),
Core\Crypto\PublicKey::read(
Core\Encoding::fromHex('fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac'),
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Raw,
),
];
// The first key agrees a shared secret, the second one checks signatures.
foreach ($sent as Core\Crypto\PublicKey $key) {
if ($key->kind() == Core\Crypto\KeyKind::X25519) {
echo 'agree a shared secret', "\n";
} else {
echo 'check a signature', "\n";
}
}
Output
agree a shared secret
check a signature