Hmac   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sign() 0 13 4
A verify() 0 4 1
1
<?php namespace nyx\auth\signers;
2
3
// Internal dependencies
4
use nyx\auth;
5
6
/**
7
 * HMAC Signer
8
 *
9
 * @package     Nyx\Auth
10
 * @version     0.1.0
11
 * @author      Michal Chojnacki <[email protected]>
12
 * @copyright   2012-2017 Nyx Dev Team
13
 * @link        https://github.com/unyx/nyx
14
 */
15
abstract class Hmac extends auth\Signer
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20
    const METHOD = 'hmac';
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function sign(string $payload, $key) : string
26
    {
27
        if ($key instanceof auth\interfaces\Credentials) {
28
            $key = $key->getSecret();
29
        }
30
31
        // At this point we expect to have a non-empty string as the key.
32
        if (!is_string($key) || empty($key)) {
33
            throw new \InvalidArgumentException('Invalid signing key provided.');
34
        }
35
36
        return hash_hmac($this->getAlgorithm(), $payload, $key, true);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function verify(string $expected, string $payload, $key) : bool
43
    {
44
        return hash_equals($expected, $this->sign($payload, $key));
45
    }
46
}
47