the raw 32 key bytes */ public static function parsePublicKey( string $text ): Result { if ( 0 !== strncmp( $text, self::PREFIX, strlen( self::PREFIX ) ) ) { return Result::err( 'public_key_malformed', 'a public key is prefixed ' . self::PREFIX ); } $bytes = Base64Url::decode( substr( $text, strlen( self::PREFIX ) ) ); if ( ! $bytes->isOk() ) { return Result::err( 'public_key_malformed', $bytes->error()->message() ); } if ( self::PUBLIC_KEY_BYTES !== strlen( $bytes->value() ) ) { return Result::err( 'public_key_malformed', 'an Ed25519 public key is 32 bytes' ); } return Result::ok( $bytes->value() ); } public static function encodePublicKey( string $raw ): string { return self::PREFIX . Base64Url::encode( $raw ); } public static function fingerprint( string $raw_public_key ): string { return 'sha256:' . hash( 'sha256', $raw_public_key ); } /** * Parses a map of kid => encoded public key, refusing the whole set if any * member is malformed. A half-loaded key set is how a site ends up trusting * fewer keys than its operator believes it does. * * @param array $encoded * @return Result Result> */ public static function parseKeySet( array $encoded ): Result { $out = array(); foreach ( $encoded as $kid => $text ) { // Array indices are strings or integers; PHP coerces a kid like "12". $kid = (string) $kid; if ( '' === $kid || ! is_string( $text ) ) { return Result::err( 'public_key_malformed', 'a key set is kid => encoded public key' ); } $parsed = Keys::parsePublicKey( $text ); if ( ! $parsed->isOk() ) { return $parsed; } $out[ $kid ] = $parsed->value(); } return Result::ok( $out ); } }