Skip to content

Core\Passkey\Credential::Algorithm

The signature algorithm of a passkey: ES256 or Ed25519.

Core\Passkey\Credential::Algorithm is declared in the class Core\Passkey\Credential. The method algorithm() of a passkey returns it. Core\Passkey\Credential::of needs it as an argument, and checks that the public key is a key of that algorithm.

ES256 is ECDSA with the P-256 curve and SHA-256. Almost every device supports it. Ed25519 is the Ed25519 signature algorithm, and some devices support it. A passkey with any other algorithm cannot be registered, so these two cases are all that a saved passkey can have.

Good to know: save the case with the other values of the passkey. A match over the two cases turns it into text for a database, and another match turns the text back into the case.

Examples

Which algorithm a passkey has

<?nvs
// Shows how to read the algorithm of a passkey and how to save it as text.
class Algorithms
{
// Returns the text to save in a database for each case.
public static function name(Core\Passkey\Credential::Algorithm $algorithm): string
{
return match ($algorithm) {
Core\Passkey\Credential::Algorithm::ES256 => 'ES256',
Core\Passkey\Credential::Algorithm::Ed25519 => 'Ed25519',
};
}
}
// An ES256 key and an Ed25519 key, in the COSE format that WebAuthn uses.
bytes $es256Key = Core\Encoding::fromHex(
'a5010203262001215820'
. '6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'
. '225820'
. '4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5',
);
bytes $ed25519Key = Core\Encoding::fromHex(
'a4010103272006215820d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a',
);
array<Core\Passkey\Credential> $passkeys = [
Core\Passkey\Credential::of(Core\Encoding::fromHex('01'), $es256Key, Core\Passkey\Credential::Algorithm::ES256, 0),
Core\Passkey\Credential::of(Core\Encoding::fromHex('02'), $ed25519Key, Core\Passkey\Credential::Algorithm::Ed25519, 0),
];
// `algorithm()` returns the case. This prints `ES256` and then `Ed25519`.
foreach ($passkeys as Core\Passkey\Credential $passkey) {
echo Core\Encoding::toHex($passkey->id()), ': ', Algorithms::name($passkey->algorithm()), "\n";
}
// The cases can be compared with `==`. This prints `true`.
echo $passkeys[1]->algorithm() == Core\Passkey\Credential::Algorithm::Ed25519 ? 'true' : 'false', "\n";
Output
01: ES256
02: Ed25519
true