Completed
Push — master ( 5ad39d...f36908 )
by Dan
03:26
created

HashHmac::getPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ds\Authenticate;
4
5
/**
6
 * Hmac hashing functions.
7
 *
8
 * PHP version 5
9
 *
10
 * @package Ds\Authenticate
11
 * @author  Dan Smith <[email protected]>
12
 * @license MIT
13
 * @license https://opensource.org/licenses/MIT
14
 * @version v0.0.1
15
 * @link    https://github.com/djsmithme/Authenticate
16
 */
17
class HashHmac
18
{
19
20
    /**
21
     * Hashing Algorithm.
22
     *
23
     * @var string
24
     */
25
    private $algorithm;
26
27
    /**
28
     * Private Key.
29
     *
30
     * @var string
31
     */
32
    private $privateKey;
33
34
    private $payload;
35
36
    /**
37
     * Date Time Zone.
38
     *
39
     * @var \DateTimeZone
40
     */
41
    private $timezone;
42
43
    /**
44
     * BasicAuth constructor.
45
     *
46
     * @param string $privateKey
47
     * @param string $algo
48
     * @param string $timezone
49
     */
50
    public function __construct($privateKey, $algo = 'sha512', $timezone = 'UTC')
51
    {
52
        $this->privateKey = $privateKey;
53
        $this->algorithm = $algo;
54
        $this->timezone = new \DateTimeZone($timezone);
55 19
    }
56
57 19
    /**
58 19
     * Creates hash from payload.
59 19
     *
60 19
     * @param  array $payload
61
     * @return mixed
62
     */
63
    public function createHash($algorithm, array $payload = [], $privateKey)
64
    {
65
66
        $this->payload = json_encode($payload);
67
68 3
        return hash_hmac(
69
            $algorithm,
70 3
            $this->payload,
71
            $privateKey
72 3
        );
73 3
    }
74
75 3
    public function getEncodedHash(array $payload = []){
76
        $hash = $this->createHash($this->algorithm, $payload);
0 ignored issues
show
Bug introduced by
The call to createHash() misses a required argument $privateKey.

This check looks for function calls that miss required arguments.

Loading history...
77 3
        return base64_encode($this->algorithm.'='.$hash);
78 3
    }
79 3
80 3
81 3
    /**
82
     * Compare received hash against payload.
83 3
     *
84
     * @param  $encodedHash
85
     * @param  array       $payload
86
     * @return bool
87
     * @throws \Exception
88
     */
89
    public function compareHash($encodedHash, array $payload = [])
90
    {
91
        $decodedHash = base64_decode($encodedHash);
92
93
        list($algo, $hash) = array_pad( explode('=', $decodedHash), 4, $this->algorithm);
94 4
        $payloadHash = $this->createHash($this->algorithm, $payload, $this->privateKey);
95
96 4
97
        try {
98 4
            $this->verifyAlgorithm($algo);
99
            $this->verifyHash($hash, $payloadHash);
100
        } catch (\Exception $e) {
101 4
            throw new \Exception($e->getMessage());
102 2
        }
103 4
104 2
        return true;
105
106
    }
107 2
108 2
109 2
    /**
110 2
     * @param $key
111 2
     * @return $this
112
     */
113 2
    public function setPrivateKey($key)
114 1
    {
115
        $new = clone $this;
116
        $new->privateKey = $key;
117 1
        return $new;
118
    }
119
120
    /**
121
     * Return current Algorithm
122
     *
123
     * @return string
124
     */
125
    public function getAlgorithm()
126 1
    {
127
        return $this->algorithm;
128 1
    }
129 1
130
131
    public function getPayload(){
132
        return $this->payload;
133
    }
134
135
    /**
136 2
     * Returns Current Private Key
137
     *
138 2
     * @return string
139
     */
140
    public function getPrivateKey()
141
    {
142
        return $this->privateKey;
143
    }
144
145
    /**
146 1
     * @param string $hash
147
     * @param string $payloadHash
148 1
     * @return bool
149
     * @throws \Exception
150
     */
151
    public function verifyHash($hash = '', $payloadHash = '')
152
    {
153
        if (!is_string($hash) || !is_string($payloadHash)) {
154
            throw new \Exception('hashes are not of the same type');
155
        }
156 2
        if (\md5($hash) === \md5($payloadHash)
157
            && strlen($hash) === strlen($payloadHash)
158 2
        ) {
159
            return true;
160
        }
161
        throw new \Exception('hashes do not match');
162
    }
163
164
    protected function verifyAlgorithm($algo)
165
    {
166
        foreach (hash_algos() as $validAlgo) {
167
            if ($algo === $validAlgo) {
168
                $this->algorithm = $validAlgo;
169
                return $this->algorithm;
170 6
            }
171
        }
172 6
        throw new \Exception('Algorithm is not valid');
173 1
    }
174
}
175