GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HMAC   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A platformSupportsAlgorithm() 0 4 1
A hmac() 0 22 3
A validHmac() 0 9 1
A setAlgorithm() 0 13 2
1
<?php
2
/**
3
 * HMAC.php
4
 *
5
 * @category        AngryBytes
6
 * @package         Hash
7
 * @copyright       Copyright (c) 2007-2016 Angry Bytes BV (http://www.angrybytes.com)
8
 */
9
10
namespace AngryBytes\Hash;
11
12
use \InvalidArgumentException;
13
14
/**
15
 * HMAC creator
16
 *
17
 * This class will generate hashes to be used as HMAC
18
 *
19
 * @category        AngryBytes
20
 * @package         Hash
21
 */
22
class HMAC
23
{
24
    /**
25
     * Algorithm to use
26
     *
27
     * @var string
28
     **/
29
    private $algorithm;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string $algorithm
35
     **/
36
    public function __construct($algorithm)
37
    {
38
        $this->setAlgorithm($algorithm);
39
    }
40
41
    /**
42
     * Does this platform support an algorithm?
43
     *
44
     * @param string $algorithm
45
     * @return bool
46
     **/
47
    public static function platformSupportsAlgorithm($algorithm)
48
    {
49
        return in_array($algorithm, hash_algos());
50
    }
51
52
    /**
53
     * Create an HMAC
54
     *
55
     * This method accepts multiple variables as input, but is restricted to
56
     * strings. All input will be concatenated before hashing.
57
     *
58
     * @param  string $sharedSecret
59
     * @param array $args
60
     * @return string
61
     */
62
    public function hmac($sharedSecret, ...$args)
63
    {
64
        // Get the data concatenated
65
        $data = '';
66
        foreach ($args as $index => $arg) {
67
            // Sanity check
68
            if (!is_string($arg)) {
69
                throw new InvalidArgumentException(sprintf(
70
                    'Received a non-string argument at "%s"',
71
                    $index
72
                ));
73
            }
74
75
            $data .= $arg;
76
        }
77
78
        return hash_hmac(
79
            $this->algorithm,
80
            $data,
81
            $sharedSecret
82
        );
83
    }
84
85
    /**
86
     * Check if a (received) message has a valid HMAC
87
     *
88
     * @param  string $message
89
     * @param  string $hmac
90
     * @param  string $sharedSecret
91
     * @return bool
92
     **/
93
    public function validHmac($message, $hmac, $sharedSecret)
94
    {
95
        // Compare HMAC with received message
96
        return Hash::compare(
97
            $hmac,
98
            // The HMAC as it should be for our shared secret
99
            $this->hmac($sharedSecret, $message)
100
        );
101
    }
102
103
    /**
104
     * Set the algorithm to use
105
     *
106
     * @param  string $algorithm
107
     * @return HMAC
108
     */
109
    protected function setAlgorithm($algorithm)
110
    {
111
        // Sanity check
112
        if (!self::platformSupportsAlgorithm($algorithm)) {
113
            throw new InvalidArgumentException(sprintf(
114
                '"%s" is not a supported hash algorithm on this platform'
115
            ));
116
        }
117
118
        $this->algorithm = $algorithm;
119
120
        return $this;
121
    }
122
}
123