1 | <?php |
||
11 | class HashHmac |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Hashing Algorithm. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | private $algorithm; |
||
20 | |||
21 | /** |
||
22 | * Private Key. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $privateKey; |
||
27 | |||
28 | /** |
||
29 | * Payload Data. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $payload = []; |
||
34 | |||
35 | /** |
||
36 | * Date Time Zone. |
||
37 | * |
||
38 | * @var \DateTimeZone |
||
39 | */ |
||
40 | private $timezone; |
||
41 | |||
42 | /** |
||
43 | * @param string $privateKey |
||
44 | * @param string $algo |
||
45 | * @param string $timezone |
||
46 | */ |
||
47 | 12 | public function __construct($privateKey, $algo = 'sha512', $timezone = 'UTC') |
|
48 | { |
||
49 | 12 | $this->privateKey = $privateKey; |
|
50 | 12 | $this->algorithm = $algo; |
|
51 | 12 | $this->timezone = new \DateTimeZone($timezone); |
|
52 | 12 | } |
|
53 | |||
54 | |||
55 | /** |
||
56 | * Create Hash Hmac from payload. |
||
57 | * |
||
58 | * @param array $payload |
||
59 | * @return string |
||
60 | */ |
||
61 | 6 | public function create(array $payload = []){ |
|
62 | 6 | $hash = $this->generate($this->algorithm, $payload, $this->privateKey); |
|
63 | 6 | return base64_encode($this->algorithm.'='.$hash); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Internal Function to creates hash from payload. |
||
68 | * |
||
69 | * @param array $payload |
||
70 | * @return mixed |
||
71 | */ |
||
72 | 7 | private function generate($algorithm, array $payload = [], $privateKey) |
|
73 | { |
||
74 | 7 | $this->payload = json_encode($payload); |
|
|
|||
75 | |||
76 | 7 | return hash_hmac( |
|
77 | $algorithm, |
||
78 | 7 | $this->payload, |
|
79 | $privateKey |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Compare received hash against payload. |
||
85 | * |
||
86 | * @param $encodedHash |
||
87 | * @param array $payload |
||
88 | * @return bool |
||
89 | * @throws \Exception |
||
90 | */ |
||
91 | 6 | public function verify($encodedHash, array $payload = []) |
|
109 | |||
110 | /** |
||
111 | * With Private Key. |
||
112 | * |
||
113 | * @param string $key |
||
114 | * @return $this |
||
115 | */ |
||
116 | 2 | public function withPrivateKey(string $key) |
|
117 | { |
||
118 | 2 | $new = clone $this; |
|
119 | 2 | $new->privateKey = $key; |
|
120 | 2 | return $new; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * With Payload Data |
||
125 | * |
||
126 | * @param array $payload |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function withPayload(array $payload) |
||
130 | { |
||
131 | $new = clone $this; |
||
132 | $new->payload = $payload; |
||
133 | return $new; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Return current Algorithm |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 1 | public function getAlgorithm() |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Return active payload. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getPayload(){ |
||
155 | |||
156 | /** |
||
157 | * Returns Private Key |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 2 | public function getPrivateKey() |
|
165 | |||
166 | /** |
||
167 | * @param string $hash |
||
168 | * @param string $payloadHash |
||
169 | * @return bool |
||
170 | * @throws \Exception |
||
171 | */ |
||
172 | 5 | private function verifyHash($hash = '', $payloadHash = '') |
|
187 | |||
188 | /** |
||
189 | * @param $algo |
||
190 | * @return string |
||
191 | * @throws \Exception |
||
192 | */ |
||
193 | 8 | private function verifyAlgorithm($algo) |
|
204 | } |
||
205 |
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..