|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Gandung\JWT\Adapter\ECDSAAdapter; |
|
7
|
|
|
use Gandung\JWT\Manager\KeyManager; |
|
8
|
|
|
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer; |
|
9
|
|
|
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer; |
|
10
|
|
|
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer; |
|
11
|
|
|
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ECDSAAdapterTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private function callDetermineGeneratorPointDirectly() |
|
19
|
|
|
{ |
|
20
|
|
|
$adapter = ECDSAAdapter::create(); |
|
21
|
|
|
$refl = new \ReflectionClass($adapter); |
|
22
|
|
|
$method = $refl->getMethod('determineGeneratorPoint'); |
|
23
|
|
|
$method->setAccessible(true); |
|
24
|
|
|
$method->invokeArgs($adapter, ['shit']); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testCanGetInstance() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->assertInstanceOf(ECDSAAdapter::class, ECDSAAdapter::create()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testCanSignGivenData() |
|
33
|
|
|
{ |
|
34
|
|
|
$adapter = ECDSAAdapter::create(); |
|
35
|
|
|
$key = new KeyManager; |
|
36
|
|
|
$key->setContentFromCertFile('cert/secp256.pem'); |
|
37
|
|
|
$hash = $adapter->createSigningHash('this is a text.', 'sha256'); |
|
38
|
|
|
$pubSerializer = new DerPublicKeySerializer( |
|
39
|
|
|
$adapter->getMathAdapter() |
|
40
|
|
|
); |
|
41
|
|
|
$privSerializer = new PemPrivateKeySerializer( |
|
42
|
|
|
new DerPrivateKeySerializer($adapter->getMathAdapter(), $pubSerializer) |
|
43
|
|
|
); |
|
44
|
|
|
$priv = $privSerializer->parse($key->getContent()); |
|
45
|
|
|
$sign = $adapter->sign($priv, $hash, 'sha256'); |
|
46
|
|
|
$this->assertInternalType('string', $sign); |
|
47
|
|
|
$this->assertNotEmpty($sign); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testCanVerifySignOfGivenData() |
|
51
|
|
|
{ |
|
52
|
|
|
$adapter = ECDSAAdapter::create(); |
|
53
|
|
|
$key = new KeyManager; |
|
54
|
|
|
$key->setContentFromCertFile('cert/secp256.pem'); |
|
55
|
|
|
$privCert = \Gandung\JWT\__jwt_parseECDSAPrivateCert($key->getContent()); |
|
56
|
|
|
$hash = $adapter->createSigningHash('this is a text.', 'sha256'); |
|
57
|
|
|
$publicDerSerializer = new DerPublicKeySerializer($adapter->getMathAdapter()); |
|
58
|
|
|
$pubSerializer = new PemPublicKeySerializer($publicDerSerializer); |
|
59
|
|
|
$privSerializer = new PemPrivateKeySerializer( |
|
60
|
|
|
new DerPrivateKeySerializer($adapter->getMathAdapter(), $publicDerSerializer) |
|
61
|
|
|
); |
|
62
|
|
|
$priv = $privSerializer->parse($privCert); |
|
63
|
|
|
$expectedSign = $adapter->sign($priv, $hash, 'sha256'); |
|
64
|
|
|
$this->assertInternalType('string', $expectedSign); |
|
65
|
|
|
$this->assertNotEmpty($expectedSign); |
|
66
|
|
|
|
|
67
|
|
|
$key->setContentFromCertFile('cert/secp256.pub.pem'); |
|
68
|
|
|
$pubCert = \Gandung\JWT\__jwt_parseECDSAPublicCert($key->getContent()); |
|
69
|
|
|
$pub = $pubSerializer->parse($pubCert); |
|
70
|
|
|
$isSignMatched = $adapter->verify($expectedSign, $pub, $hash, 'sha256'); |
|
71
|
|
|
$this->assertInternalType('boolean', $isSignMatched); |
|
72
|
|
|
$this->assertTrue($isSignMatched); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @expectedException \InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testCanRaiseExceptionWhenDetermineGeneratorPointWithInvalidAlgorithm() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->callDetermineGeneratorPointDirectly(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|