Core\Passkey\Credential::algorithm
$credential->algorithm(): Core\Passkey\Credential::AlgorithmReturns the signature algorithm of the passkey, as a case of Core\Passkey\Credential::Algorithm. The case is ES256 or Ed25519.
The device chooses the algorithm when the user registers the passkey. The algorithm and the public key belong together. Core\Passkey\Credential::of throws a LogicError when the public key is not a key of the algorithm.
To save the case in a database, convert it to a number with as int. ES256 is 0, and Ed25519 is 1. To read the number back, use as Core\Passkey\Credential::Algorithm. A number that is not 0 or 1 throws a RuntimeError. With as ?Core\Passkey\Credential::Algorithm, the result is null.
The examples below read the algorithm, save it as a number, and check a saved number.
Return value
Returns Core\Passkey\Credential::Algorithm. ES256 or Ed25519.
Examples
Read the algorithm
<?nvs// Shows how to read the signature algorithm of a saved passkey.
class Label{ // `match` gives a name for each case of the enum. public static function of(Core\Passkey\Credential $passkey): string { return match ($passkey->algorithm()) { Core\Passkey\Credential::Algorithm::ES256 => 'ES256, a P-256 key', Core\Passkey\Credential::Algorithm::Ed25519 => 'Ed25519, an Ed25519 key', }; }}
// The application saved these values when the user registered the passkey.Core\Passkey\Credential $passkey = Core\Passkey\Credential::of( Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16'), Core\Encoding::fromHex('a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac'), Core\Passkey\Credential::Algorithm::Ed25519, 0,);
// This prints `Ed25519, an Ed25519 key`.echo Label::of($passkey), "\n";Ed25519, an Ed25519 keySave the algorithm as a number
<?nvs// Shows how to save the algorithm as a number, and how to read the number back.
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');bytes $publicKey = Core\Encoding::fromHex( 'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',);Core\Passkey\Credential $passkey = Core\Passkey\Credential::of($id, $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 0);
// `as int` converts the case to its number. This prints `1`.int $saved = $passkey->algorithm() as int;echo $saved, "\n";
// `as Core\Passkey\Credential::Algorithm` converts the number back to the case.// The passkey is created again from the saved values. This prints `Ed25519`.Core\Passkey\Credential::Algorithm $algorithm = $saved as Core\Passkey\Credential::Algorithm;Core\Passkey\Credential $loaded = Core\Passkey\Credential::of($id, $publicKey, $algorithm, 0);echo $loaded->algorithm() == Core\Passkey\Credential::Algorithm::Ed25519 ? 'Ed25519' : 'ES256', "\n";1Ed25519Check a saved number
<?nvs// Shows how to check an algorithm number that was read from a database.
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');bytes $publicKey = Core\Encoding::fromHex( 'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',);
// `1` is the number of `Ed25519`. No case has the number `7` or `-1`.array<int> $numbers = [1, 7, -1];foreach ($numbers as int $number) { // `as ?Core\Passkey\Credential::Algorithm` gives `null` when no case has the number. ?Core\Passkey\Credential::Algorithm $algorithm = $number as ?Core\Passkey\Credential::Algorithm; if ($algorithm == null) { echo $number, ": not an algorithm\n"; continue; } Core\Passkey\Credential $passkey = Core\Passkey\Credential::of($id, $publicKey, $algorithm, 0); echo $number, ': ', $passkey->algorithm() == Core\Passkey\Credential::Algorithm::Ed25519 ? 'Ed25519' : 'ES256', "\n";}1: Ed255197: not an algorithm-1: not an algorithm