Completed
Push — develop ( b5844e...e46df6 )
by Florent
02:33
created

Ed25519SignatureTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 4
c 4
b 2
f 1
lcom 1
cbo 8
dl 0
loc 55
rs 10
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
use Base64Url\Base64Url;
13
use Jose\Algorithm\Signature\Ed25519;
14
use Jose\Factory\JWSFactory;
15
use Jose\Factory\VerifierFactory;
16
use Jose\Loader;
17
use Jose\Object\JWK;
18
use Jose\Object\JWSInterface;
19
use Jose\Test\TestCase;
20
21
/**
22
 * Class Ed25519SignatureTest.
23
 *
24
 * @group Ed25519
25
 * @group Unit
26
 */
27
class Ed25519SignatureTest extends TestCase
28
{
29
    /**
30
     * @see https://tools.ietf.org/html/draft-ietf-jose-cfrg-curves-00#appendix-A.5
31
     */
32
    public function testEd25519VerifyAlgorithm()
33
    {
34
        if (!function_exists('ed25519_sign')) {
35
            $this->markTestSkipped('Ed25519 extension not available');
36
        }
37
38
        $key = new JWK([
39
            'kty' => 'OKP',
40
            'crv' => 'Ed25519',
41
            'd'   => 'nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A',
42
            'x'   => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo',
43
        ]);
44
45
        $ed25519 = new Ed25519();
46
        $input = 'eyJhbGciOiJFZDI1NTE5In0.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc';
47
        $signature = Base64Url::decode('UxhIYLHGg39NVCLpQAVD_UcfOmnGSCzLFZoXYkLiIbFccmOb_qObsgjzLKsfJw-4NlccUgvYrEHrRbNV0HcZAQ');
48
49
        $ed25519->verify($key, $input, $signature);
50
    }
51
52
    /**
53
     * @see https://tools.ietf.org/html/draft-ietf-jose-cfrg-curves-00#appendix-A.5
54
     */
55
    public function testEd25519SignAndVerifyAlgorithm()
56
    {
57
        if (!function_exists('ed25519_sign')) {
58
            $this->markTestSkipped('Ed25519 extension not available');
59
        }
60
61
        $key = new JWK([
62
            'kty' => 'OKP',
63
            'crv' => 'Ed25519',
64
            'd'   => 'nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A',
65
            'x'   => '11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo',
66
        ]);
67
68
        $header = ['alg' => 'Ed25519'];
69
        $input = Base64Url::decode('RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc');
70
71
        $jws = JWSFactory::createJWSToCompactJSON($input, $key, $header);
72
73
        $this->assertEquals('eyJhbGciOiJFZDI1NTE5In0.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc.UxhIYLHGg39NVCLpQAVD_UcfOmnGSCzLFZoXYkLiIbFccmOb_qObsgjzLKsfJw-4NlccUgvYrEHrRbNV0HcZAQ', $jws);
74
        $loaded = Loader::load($jws);
75
        $verifier = VerifierFactory::createVerifier(['Ed25519']);
76
77
        $this->assertInstanceOf(JWSInterface::class, $loaded);
78
        $this->assertEquals(1, $loaded->countSignatures());
79
        $verifier->verifyWithKey($loaded, $key);
80
    }
81
}
82