Completed
Push — v2.0.x ( 11273c...f488cd )
by Florent
04:53 queued 01:28
created

HMACSignatureTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 87
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
 */
22
class HMACSignatureTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @expectedException \InvalidArgumentException
26
     * @expectedExceptionMessage The key is not valid
27
     */
28
    public function testInvalidKey()
29
    {
30
        $key = new JWK([
31
            'kty' => 'EC',
32
        ]);
33
34
        $hmac = new HS256();
35
        $data = 'Je suis Charlie';
36
37
        $hmac->sign($key, $data);
38
    }
39
40
    /**
41
     *
42
     */
43
    public function testSignatureHasBadBadLength()
44
    {
45
        $key = new JWK([
46
            'kty' => 'oct',
47
            'k'   => 'foo',
48
        ]);
49
        $hmac = new HS256();
50
        $data = 'Je suis Charlie';
51
52
        $this->assertFalse($hmac->verify($key, $data, hex2bin('326eb338c465d3587f3349df0b96ba81')));
53
    }
54
55
    /**
56
     *
57
     */
58
    public function testHS256SignAndVerify()
59
    {
60
        $key = new JWK([
61
            'kty' => 'oct',
62
            'k'   => 'foo',
63
        ]);
64
        $hmac = new HS256();
65
        $data = 'Je suis Charlie';
66
67
        $signature = $hmac->sign($key, $data);
68
69
        $this->assertEquals(hex2bin('326eb338c465d3587f3349df0b96ba813670376cab1dfe0fd4ce126ab50ae354'), $signature);
70
        $this->assertTrue($hmac->verify($key, $data, $signature));
71
    }
72
73
    /**
74
     *
75
     */
76
    public function testHS384SignAndVerify()
77
    {
78
        $key = new JWK([
79
            'kty' => 'oct',
80
            'k'   => 'foo',
81
        ]);
82
        $hmac = new HS384();
83
        $data = 'Je suis Charlie';
84
85
        $signature = $hmac->sign($key, $data);
86
87
        $this->assertEquals(hex2bin('7074ed8ec356ce7d61d99b86caabccc741def9f3d0881c822b775dfe91520fdcb037b1b7f8bcf425796ec209decb760e'), $signature);
88
        $this->assertTrue($hmac->verify($key, $data, $signature));
89
    }
90
91
    /**
92
     *
93
     */
94
    public function testHS512SignAndVerify()
95
    {
96
        $key = new JWK([
97
            'kty' => 'oct',
98
            'k'   => 'foo',
99
        ]);
100
        $hmac = new HS512();
101
        $data = 'Je suis Charlie';
102
103
        $signature = $hmac->sign($key, $data);
104
105
        $this->assertEquals(hex2bin('13d07b012d7a31369a0c12eccaeb40e79e5e2d11183a00f977fce075722206f45cdd77096d7ed719626868701076654cf03565c25a3f6b9e698e466717c3b0ca'), $signature);
106
        $this->assertTrue($hmac->verify($key, $data, $signature));
107
    }
108
}
109