Completed
Push — master ( f36908...d7724a )
by Dan
03:01
created

HashHmac::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 1
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
    /**
35
     * Payload Data.
36
     *
37
     * @var array
38
     */
39
    private $payload = [];
40
41
    /**
42
     * Date Time Zone.
43
     *
44
     * @var \DateTimeZone
45
     */
46
    private $timezone;
47
48
    /**
49
     * @param string $privateKey
50
     * @param string $algo
51
     * @param string $timezone
52
     */
53
    public function __construct($privateKey, $algo = 'sha512', $timezone = 'UTC')
54
    {
55 19
        $this->privateKey = $privateKey;
56
        $this->algorithm = $algo;
57 19
        $this->timezone = new \DateTimeZone($timezone);
58 19
    }
59 19
60 19
    /**
61
     * Creates hash from payload.
62
     * @param  array $payload
63
     * @return mixed
64
     */
65
    public function create($algorithm, array $payload = [], $privateKey)
66
    {
67
68 3
        $this->payload = json_encode($payload);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_encode($payload) of type string is incompatible with the declared type array of property $payload.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
70 3
        return hash_hmac(
71
            $algorithm,
72 3
            $this->payload,
73 3
            $privateKey
74
        );
75 3
    }
76
77 3
    public function getEncodedHash(array $payload = []){
78 3
        $hash = $this->createHash($this->algorithm, $payload);
0 ignored issues
show
Bug introduced by
The method createHash() does not exist on Ds\Authenticate\HashHmac. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
79 3
        return base64_encode($this->algorithm.'='.$hash);
80 3
    }
81 3
82
83 3
    /**
84
     * Compare received hash against payload.
85
     *
86
     * @param  $encodedHash
87
     * @param  array       $payload
88
     * @return bool
89
     * @throws \Exception
90
     */
91
    public function compareHash($encodedHash, array $payload = [])
92
    {
93
        $decodedHash = base64_decode($encodedHash);
94 4
95
        list($algo, $hash) = array_pad( explode('=', $decodedHash), 4, $this->algorithm);
96 4
        $payloadHash = $this->createHash($this->algorithm, $payload, $this->privateKey);
0 ignored issues
show
Bug introduced by
The method createHash() does not exist on Ds\Authenticate\HashHmac. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
        
98 4
        try {
99
            $this->verifyAlgorithm($algo);
100
            $this->verifyHash($hash, $payloadHash);
0 ignored issues
show
Bug introduced by
The method verifyHash() does not exist on Ds\Authenticate\HashHmac. Did you maybe mean verify()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101 4
        } catch (\Exception $e) {
102 2
            throw new \Exception($e->getMessage());
103 4
        }
104 2
105
        return true;
106
107 2
    }
108 2
109 2
110 2
    /**
111 2
     * @param $key
112
     * @return $this
113 2
     */
114 1
    public function setPrivateKey($key)
115
    {
116
        $new = clone $this;
117 1
        $new->privateKey = $key;
118
        return $new;
119
    }
120
121
    /**
122
     * Return current Algorithm
123
     *
124
     * @return string
125
     */
126 1
    public function getAlgorithm()
127
    {
128 1
        return $this->algorithm;
129 1
    }
130
131
132
    public function getPayload(){
133
        return $this->payload;
134
    }
135
136 2
    /**
137
     * Returns Current Private Key
138 2
     *
139
     * @return string
140
     */
141
    public function getPrivateKey()
142
    {
143
        return $this->privateKey;
144
    }
145
146 1
    /**
147
     * @param string $hash
148 1
     * @param string $payloadHash
149
     * @return bool
150
     * @throws \Exception
151
     */
152
    public function verify($hash = '', $payloadHash = '')
153
    {
154
        if (!is_string($hash) || !is_string($payloadHash)) {
155
            throw new \Exception('hashes are not of the same type');
156 2
        }
157
        if (\md5($hash) === \md5($payloadHash)
158 2
            && strlen($hash) === strlen($payloadHash)
159
        ) {
160
            return true;
161
        }
162
        throw new \Exception('hashes do not match');
163
    }
164
165
    protected function verifyAlgorithm($algo)
166
    {
167
        foreach (hash_algos() as $validAlgo) {
168
            if ($algo === $validAlgo) {
169
                $this->algorithm = $validAlgo;
170 6
                return $this->algorithm;
171
            }
172 6
        }
173 1
        throw new \Exception('Algorithm is not valid');
174
    }
175
}
176