Skip to content

Commit

Permalink
Revert "Remove extra \0 byte if present (#476)" (#477)
Browse files Browse the repository at this point in the history
This reverts commit 4ab8741.
  • Loading branch information
Spomky authored Aug 23, 2023
1 parent 4ab8741 commit 36bf60a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/Component/Core/Util/ECKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,9 @@ private static function getKey(JWK $jwk): string
if (! is_string($y)) {
throw new InvalidArgumentException('Unable to get the public key');
}
$binX = ltrim(Base64UrlSafe::decode($x), "\0");
$binY = ltrim(Base64UrlSafe::decode($y), "\0");

return "\04"
. str_pad($binX, $length, "\0", STR_PAD_LEFT)
. str_pad($binY, $length, "\0", STR_PAD_LEFT)
;
. str_pad(Base64UrlSafe::decode($x), $length, "\0", STR_PAD_LEFT)
. str_pad(Base64UrlSafe::decode($y), $length, "\0", STR_PAD_LEFT);
}
}
25 changes: 0 additions & 25 deletions tests/Component/Core/JWKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use InvalidArgumentException;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\ECKey;
use const JSON_THROW_ON_ERROR;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -128,28 +127,4 @@ public function iCanConvertAPrivateKeyIntoPublicKey(): void
'kid' => '9876543210',
]), json_encode($public, JSON_THROW_ON_ERROR));
}

/**
* @test
* @see https://github.com/web-token/jwt-framework/issues/475
*/
public static function convertToPEM(): void
{
// Given
$key = '{"kty":"EC","crv":"P-256","x":"GDDdmNtwNvlXN04SEUp20BZJ9im6SQqkP8u4d8G6RAk","y":"AIAxkBwTTqbCcNbqbpk8l_Eh-4KtpgyyHkNJ6K4jnvOv","use":"sig","alg":"ES256","kid":"ayRrlw","key_ops":["verify"]}';
$jwk = JWK::createFromJson($key);
$expectedPEM = <<<'PEM'
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEGDDdmNtwNvlXN04SEUp20BZJ9im6
SQqkP8u4d8G6RAmAMZAcE06mwnDW6m6ZPJfxIfuCraYMsh5DSeiuI57zrw==
-----END PUBLIC KEY-----

PEM;

//When
$pem = ECKey::convertToPEM($jwk);

//Then
static::assertSame($expectedPEM, $pem);
}
}

0 comments on commit 36bf60a

Please sign in to comment.