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

HMACSignatureTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 5
dl 0
loc 75
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 Jose\Algorithm\Signature\HS256;
13
use Jose\Algorithm\Signature\HS384;
14
use Jose\Algorithm\Signature\HS512;
15
use Jose\Object\JWK;
16
17
/**
18
 * Class HMACSignatureTest.
19
 *
20
 * @group HMAC
21
 * @group Unit
22
 */
23
class HMACSignatureTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @expectedException \InvalidArgumentException
27
     * @expectedExceptionMessage Wrong key type.
28
     */
29
    public function testInvalidKey()
30
    {
31
        $key = new JWK([
32
            'kty' => 'EC',
33
        ]);
34
35
        $hmac = new HS256();
36
        $data = 'Live long and Prosper.';
37
38
        $hmac->sign($key, $data);
39
    }
40
41
    public function testSignatureHasBadBadLength()
42
    {
43
        $key = new JWK([
44
            'kty' => 'oct',
45
            'k'   => 'foo',
46
        ]);
47
        $hmac = new HS256();
48
        $data = 'Live long and Prosper.';
49
50
        $this->assertFalse($hmac->verify($key, $data, hex2bin('326eb338c465d3587f3349df0b96ba81')));
51
    }
52
53
    public function testHS256SignAndVerify()
54
    {
55
        $key = new JWK([
56
            'kty' => 'oct',
57
            'k'   => 'foo',
58
        ]);
59
        $hmac = new HS256();
60
        $data = 'Live long and Prosper.';
61
62
        $signature = $hmac->sign($key, $data);
63
64
        $this->assertEquals(hex2bin('89f750759cb8ad9315d7ec6bd8d5dc5899e0a97bc12f9e355f383776f53f025c'), $signature);
65
        $this->assertTrue($hmac->verify($key, $data, $signature));
66
    }
67
68
    public function testHS384SignAndVerify()
69
    {
70
        $key = new JWK([
71
            'kty' => 'oct',
72
            'k'   => 'foo',
73
        ]);
74
        $hmac = new HS384();
75
        $data = 'Live long and Prosper.';
76
77
        $signature = $hmac->sign($key, $data);
78
79
        $this->assertEquals(hex2bin('8985f2c6efef1c1b9baf7d7b0b17ce6db65184044bdeaa01296fe6d61900224fc783f4bb7b7aadfdfb4d0663b1284e66'), $signature);
80
        $this->assertTrue($hmac->verify($key, $data, $signature));
81
    }
82
83
    public function testHS512SignAndVerify()
84
    {
85
        $key = new JWK([
86
            'kty' => 'oct',
87
            'k'   => 'foo',
88
        ]);
89
        $hmac = new HS512();
90
        $data = 'Live long and Prosper.';
91
92
        $signature = $hmac->sign($key, $data);
93
94
        $this->assertEquals(hex2bin('6f91ca09dc2e655d089f1018fb447f16c68d65f32f54ea84542edb1db5dfbbda141cbb41741b7383a7dff6af56be564fd74a8857eab6a680094bbcb41b2f29e1'), $signature);
95
        $this->assertTrue($hmac->verify($key, $data, $signature));
96
    }
97
}
98