Skip to content

Commit 07c292b

Browse files
authored
Fix RSA key (#36)
1 parent fb53f10 commit 07c292b

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/Key/RsaKey.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Brick\Math\BigInteger;
99
use function in_array;
1010
use InvalidArgumentException;
11+
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo;
1112
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\RSA\RSAPrivateKey;
1213
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\RSA\RSAPublicKey;
1314

@@ -208,8 +209,9 @@ public function asPem(): string
208209
$this->binaryToBigInteger($this->n()),
209210
$this->binaryToBigInteger($this->e())
210211
);
212+
$rsaKey = PublicKeyInfo::fromPublicKey($publicKey);
211213

212-
return $publicKey->toPEM()
214+
return $rsaKey->toPEM()
213215
->string();
214216
}
215217

tests/Key/RSA-Public.pem

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsWCJwDvzAQ2ssuX7GIQJ
3+
n5VO4cOGi4MJe6A0mzwA+/YdZlCw5tJaOZcSeLiFunECdJtuI9ldcQasF8ZsGqLS
4+
r98O25WdGHiD3R+z4v0KW8pkJaDVAL2hZSkFlyUJ2y6Vfvndpe0oe2aCsIXdEmHS
5+
O0k4da4bGWNCBNWGuzCV9Uf++t3rzLBi9kOtnSrlTfEpnxArWuhySQwJDeQLhBKd
6+
mugULQugVfTnpISK23Wq3hkOfz7XyLmAgLIRhE4rwsiDtC0cYRA7r9iip3Vc8h2x
7+
AV5y0+1g4+uN5KFV4zDxqBy98V43h5sZJ6UBcJH36t6ysdD5ux92SrpPeazcSTCq
8+
EwIDAQAB
9+
-----END PUBLIC KEY-----

tests/Key/RSAKeyTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cose\Tests\Key;
6+
7+
use Cose\Algorithm\Signature\RSA\RS256;
8+
use Cose\Key\RsaKey;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class RSAKeyTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function theKeyIsCorrectlyEncoded(): void
17+
{
18+
// Given
19+
$key = RsaKey::create([
20+
RsaKey::TYPE => RsaKey::TYPE_RSA,
21+
RsaKey::ALG => RS256::ID,
22+
RsaKey::DATA_N => base64_decode(
23+
'sWCJwDvzAQ2ssuX7GIQJn5VO4cOGi4MJe6A0mzwA+/YdZlCw5tJaOZcSeLiFunECdJtuI9ldcQasF8ZsGqLSr98O25WdGHiD3R+z4v0KW8pkJaDVAL2hZSkFlyUJ2y6Vfvndpe0oe2aCsIXdEmHSO0k4da4bGWNCBNWGuzCV9Uf++t3rzLBi9kOtnSrlTfEpnxArWuhySQwJDeQLhBKdmugULQugVfTnpISK23Wq3hkOfz7XyLmAgLIRhE4rwsiDtC0cYRA7r9iip3Vc8h2xAV5y0+1g4+uN5KFV4zDxqBy98V43h5sZJ6UBcJH36t6ysdD5ux92SrpPeazcSTCqEw',
24+
true
25+
),
26+
RsaKey::DATA_E => base64_decode('AQAB', true),
27+
]);
28+
$expected = trim(file_get_contents(__DIR__ . '/RSA-Public.pem'));
29+
30+
// When
31+
$pem = $key->toPublic()
32+
->asPem();
33+
34+
// Then
35+
static::assertSame($expected, $pem);
36+
}
37+
}

0 commit comments

Comments
 (0)