Skip to content

Core\Passkey\Credential::publicKey

$credential->publicKey(): bytes

Returns the public key of the passkey, as bytes. The bytes are in the COSE format, which is the key format that WebAuthn uses.

The device sends this key when the user registers the passkey. At each sign-in, Core\Passkey::verifySignIn checks the signature with it. An Ed25519 key is usually 42 bytes, and an ES256 key is usually 77 bytes.

Save the bytes unchanged, with the other three values of the passkey. Pass them to Core\Passkey\Credential::of to create the passkey again. of checks that the bytes are a key of the saved algorithm. If they are not, it throws a LogicError.

Good to know: the key is public, so it does not need to be secret. But nobody else may change it. Somebody who replaces the saved key with their own key can sign in to the account. Save it where only your application can write.

The examples below read the key, compare the two kinds of key, and save the key as text.

Return value

Returns bytes. The key as bytes. Save them unchanged.

Examples

Read the public key

<?nvs
// Shows how to read the public key of a saved passkey.
// The application saved these values when the user registered the passkey.
bytes $publicKey = Core\Encoding::fromHex(
'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',
);
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of(
Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16'),
$publicKey,
Core\Passkey\Credential::Algorithm::Ed25519,
0,
);
// `publicKey()` returns the bytes that were passed to `of`. This prints `42 bytes`.
echo Core\Bytes::length($passkey->publicKey()), " bytes\n";
// The first 10 bytes say that this is an Ed25519 key. The last 32 bytes are the key itself.
echo Core\Encoding::toHex(Core\Bytes::slice($passkey->publicKey(), 0, 10)), "\n";
echo Core\Encoding::toHex(Core\Bytes::slice($passkey->publicKey(), 10)), "\n";
Output
42 bytes
a4010103272006215820
fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac

Two kinds of key

<?nvs
// Shows the public keys of the two kinds of passkey, `ES256` and `Ed25519`.
// This class makes a COSE key from a key of `Core\Crypto`. In real use, the device sends this key.
final class Device
{
public static function ed25519(): bytes
{
bytes $raw = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519)->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
return Core\Bytes::join([Core\Encoding::fromHex('a4010103272006215820'), $raw]);
}
public static function es256(): bytes
{
bytes $raw = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256)->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
return Core\Bytes::join([
Core\Encoding::fromHex('a5010203262001215820'),
Core\Bytes::slice($raw, 1, 32),
Core\Encoding::fromHex('225820'),
Core\Bytes::slice($raw, 33, 32),
]);
}
}
bytes $id = Core\Random::bytes(16);
Core\Passkey\Credential $phone = Core\Passkey\Credential::of($id, Device::es256(), Core\Passkey\Credential::Algorithm::ES256, 0);
Core\Passkey\Credential $laptop = Core\Passkey\Credential::of($id, Device::ed25519(), Core\Passkey\Credential::Algorithm::Ed25519, 0);
// An ES256 key has two numbers of 32 bytes. An Ed25519 key has one.
// This prints `ES256: 77 bytes` and `Ed25519: 42 bytes`.
echo 'ES256: ', Core\Bytes::length($phone->publicKey()), " bytes\n";
echo 'Ed25519: ', Core\Bytes::length($laptop->publicKey()), " bytes\n";
Output
ES256: 77 bytes
Ed25519: 42 bytes

Save the key as text

<?nvs
// Shows how to save the public key as text, and what happens when the saved text was changed.
// This makes an ES256 passkey with a new key. In real use, the device sends the key.
bytes $raw = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256)->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
bytes $publicKey = Core\Bytes::join([
Core\Encoding::fromHex('a5010203262001215820'),
Core\Bytes::slice($raw, 1, 32),
Core\Encoding::fromHex('225820'),
Core\Bytes::slice($raw, 33, 32),
]);
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of($id, $publicKey, Core\Passkey\Credential::Algorithm::ES256, 0);
// `toBase64Url` converts the key to text, and `fromBase64Url` converts it back.
// The passkey is created again with the same key. This prints `same key`.
string $text = Core\Encoding::toBase64Url($passkey->publicKey());
Core\Passkey\Credential $loaded = Core\Passkey\Credential::of($id, Core\Encoding::fromBase64Url($text), Core\Passkey\Credential::Algorithm::ES256, 0);
echo $loaded->publicKey() == $passkey->publicKey() ? 'same key' : 'another key', "\n";
// The last byte of the saved key was changed. The bytes are not a valid ES256 key now,
// so `of` throws a `LogicError`.
bytes $changed = Core\Bytes::join([
Core\Bytes::slice($passkey->publicKey(), 0, 76),
Core\Bytes::pack('C', 255 - Core\Bytes::at($passkey->publicKey(), 76)),
]);
try {
Core\Passkey\Credential::of($id, $changed, Core\Passkey\Credential::Algorithm::ES256, 0);
echo "the changed key was accepted\n";
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
same key
Core\Passkey\Credential::of(): the ES256 key is not a point on the P-256 curve.