HmacUsingSha::verify()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the tmilos/jose-jwt package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\JoseJwt\Jws;
13
14
use Tmilos\JoseJwt\Error\JoseJwtException;
15
16
class HmacUsingSha implements JwsAlgorithm
17
{
18
    /** @var string */
19
    private $hashMethod;
20
21
    /**
22
     * @param string $hashMethod
23
     */
24
    public function __construct($hashMethod)
25
    {
26
        $this->hashMethod = $hashMethod;
27
    }
28
29
    /**
30
     * @param string $securedInput
31
     * @param string $key
32
     *
33
     * @return string
34
     */
35
    public function sign($securedInput, $key)
36
    {
37
        if (null === $key || trim($key) === '') {
38
            throw new JoseJwtException('Hmac key can not be empty');
39
        }
40
41
        return hash_hmac($this->hashMethod, $securedInput, $key, true);
42
    }
43
44
    /**
45
     * @param string $signature
46
     * @param string $securedInput
47
     * @param string $key
48
     *
49
     * @return bool
50
     */
51
    public function verify($signature, $securedInput, $key)
52
    {
53
        if (null == $key || trim($key) === '') {
54
            throw new JoseJwtException('Hmac key can not be empty');
55
        }
56
57
        $calculatedSignature = hash_hmac($this->hashMethod, $securedInput, $key, true);
58
59
        return $signature === $calculatedSignature;
60
    }
61
}
62