Failed Conditions
Push — master ( 339400...89d9e2 )
by Florent
01:13
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
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace AESKW\Tests;
15
16
use AESKW\A128KW;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * This test comes from the JWE specification.
21
 *
22
 * @see https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-39#appendix-A.3.3
23
 */
24
final class JWETest extends TestCase
25
{
26
    /**
27
     * @test
28
     */
29
    public function cEKEncryption()
30
    {
31
        // The KEK
32
        $kek = base64_decode('GawgguFyGrWKav7AX4VKUg', true);
33
34
        // The CEK to encrypt (we convert it into a binary string)
35
        $data = [4, 211, 31, 197, 84, 157, 252, 254, 11, 100, 157, 250, 63, 170, 106, 206, 107, 124, 212, 45, 111, 107, 9, 219, 200, 177, 0, 240, 143, 156, 44, 207];
36
        foreach ($data as $key => $value) {
37
            $data[$key] = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
38
        }
39
        $data = hex2bin(implode('', $data));
40
41
        $wrapped = A128KW::wrap($kek, $data);
42
        static::assertEquals(base64_decode('6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ', true), $wrapped);
43
        $unwrapped = A128KW::unwrap($kek, $wrapped);
44
        static::assertEquals($data, $unwrapped);
45
    }
46
}
47