|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Algorithm\ECDSA; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Gandung\JWT\Adapter\ECDSAAdapter; |
|
7
|
|
|
use Gandung\JWT\Algorithm\ECDSA\ES256; |
|
8
|
|
|
use Gandung\JWT\Manager\KeyManager; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class ES256Test extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testCanGetInstance() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->assertInstanceOf(ES256::class, new ES256(ECDSAAdapter::create())); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testCanSignGivenData() |
|
21
|
|
|
{ |
|
22
|
|
|
$key = new KeyManager; |
|
23
|
|
|
$key->setContentFromCertFile('cert/secp256.pem'); |
|
24
|
|
|
$es256 = new ES256(ECDSAAdapter::create()); |
|
25
|
|
|
$signature = $es256->sign('this is a text.', $key); |
|
26
|
|
|
$this->assertInternalType('string', $signature); |
|
27
|
|
|
$this->assertNotEmpty($signature); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testCanVerifySignature() |
|
31
|
|
|
{ |
|
32
|
|
|
$key = new KeyManager; |
|
33
|
|
|
$key->setContentFromCertFile('cert/secp256.pem'); |
|
34
|
|
|
$es256 = new ES256(ECDSAAdapter::create()); |
|
35
|
|
|
$signature = $es256->sign('this is a text.', $key); |
|
36
|
|
|
$this->assertInternalType('string', $signature); |
|
37
|
|
|
$this->assertNotEmpty($signature); |
|
38
|
|
|
$key->setContentFromCertFile('cert/secp256.pub.pem'); |
|
39
|
|
|
$isSignatureMatched = $es256->verify($signature, 'this is a text.', $key); |
|
40
|
|
|
$this->assertInternalType('boolean', $isSignatureMatched); |
|
41
|
|
|
$this->assertTrue($isSignatureMatched); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testCanGetAlgorithmAlias() |
|
45
|
|
|
{ |
|
46
|
|
|
$es256 = new ES256(ECDSAAdapter::create()); |
|
47
|
|
|
$this->assertEquals(\Gandung\JWT\Token\Algorithm::ES256, $es256->getAlgorithmAlias()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|