Passed
Pull Request — master (#14)
by Florent
06:01 queued 02:47
created

JWETest::cEKEncryption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AESKW\Tests;
6
7
use AESKW\A128KW;
8
use PHPUnit\Framework\TestCase;
9
use const STR_PAD_LEFT;
10
11
/**
12
 * This test comes from the JWE specification.
13
 *
14
 * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.3.3
15
 *
16
 * @internal
17
 */
18
final class JWETest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function cEKEncryption(): void
24
    {
25
        // The KEK
26
        $kek = base64_decode('GawgguFyGrWKav7AX4VKUg', true);
27
28
        // The CEK to encrypt (we convert it into a binary string)
29
        $data = [
30
            4,
31
            211,
32
            31,
33
            197,
34
            84,
35
            157,
36
            252,
37
            254,
38
            11,
39
            100,
40
            157,
41
            250,
42
            63,
43
            170,
44
            106,
45
            206,
46
            107,
47
            124,
48
            212,
49
            45,
50
            111,
51
            107,
52
            9,
53
            219,
54
            200,
55
            177,
56
            0,
57
            240,
58
            143,
59
            156,
60
            44,
61
            207,
62
        ];
63
        foreach ($data as $key => $value) {
64
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
65
        }
66
        $data = hex2bin(implode('', $data));
67
68
        $wrapped = A128KW::wrap($kek, $data);
69
        static::assertSame(base64_decode('6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ', true), $wrapped);
70
        $unwrapped = A128KW::unwrap($kek, $wrapped);
71
        static::assertSame($data, $unwrapped);
72
    }
73
}
74