1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
namespace PragmaRX\Google2FA;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use PragmaRX\Google2FA\Support\Base32;
use PragmaRX\Google2FA\Support\Constants;
use PragmaRX\Google2FA\Support\QRCode;
class Google2FA
{
use QRCode, Base32;
/**
* Length of the Token generated.
*/
protected $oneTimePasswordLength = 6;
/**
* Interval between key regeneration.
*/
protected $keyRegeneration = 30;
/**
* Secret.
*/
protected $secret;
/**
* Window.
*/
protected $window = 1; // Keys will be valid for 60 seconds
/**
* Find a valid One Time Password.
*
* @param $secret
* @param $key
* @param $window
* @param $startingTimestamp
* @param $timestamp
* @param string $oldTimestamp
*
* @return bool
*/
public function findValidOTP($secret, $key, $window, $startingTimestamp, $timestamp, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
{
for (; $startingTimestamp <= $timestamp + $this->getWindow($window); $startingTimestamp++) {
if (hash_equals($this->oathHotp($secret, $startingTimestamp), $key)) {
return
$oldTimestamp === Constants::ARGUMENT_NOT_SET
? true
: $startingTimestamp;
}
}
return false;
}
/**
* Generate a digit secret key in base32 format.
*
* @param int $length
* @param string $prefix
*
* @return string
*/
public function generateSecretKey($length = 16, $prefix = '')
{
return $this->generateBase32RandomKey($length, $prefix);
}
/**
* Get the current one time password for a key.
*
* @param $secret
*
* @return string
*/
public function getCurrentOtp($secret)
{
return $this->oathHotp($secret, $this->getTimestamp());
}
/**
* Get key regeneration.
*
* @return mixed
*/
public function getKeyRegeneration()
{
return $this->keyRegeneration;
}
/**
* Get OTP length.
*
* @return mixed
*/
public function getOneTimePasswordLength()
{
return $this->oneTimePasswordLength;
}
/**
* Get secret.
*
* @param string|null $secret
*
* @return mixed
*/
public function getSecret($secret = null)
{
return
is_null($secret)
? $this->secret
: $secret;
}
/**
* Returns the current Unix Timestamp divided by the $keyRegeneration
* period.
*
* @return int
**/
public function getTimestamp()
{
return (int) floor(microtime(true) / $this->keyRegeneration);
}
/**
* Get the OTP window.
*
* @param null|int $window
*
* @return mixed
*/
public function getWindow($window = null)
{
return
is_null($window)
? $this->window
: $window;
}
/**
* Make a window based starting timestamp.
*
* @param $window
* @param $timestamp
* @param $oldTimestamp
*
* @return mixed
*/
private function makeStartingTimestamp($window, $timestamp, $oldTimestamp)
{
return $oldTimestamp === Constants::ARGUMENT_NOT_SET
? $timestamp - $this->getWindow($window)
: max($timestamp - $this->getWindow($window), $oldTimestamp + 1);
}
/**
* Get/use a starting timestamp for key verification.
*
* @param string|int|null $timestamp
*
* @return int
*/
protected function makeTimestamp($timestamp = null)
{
if (is_null($timestamp)) {
return $this->getTimestamp();
}
return (int) $timestamp;
}
/**
* Takes the secret key and the timestamp and returns the one time
* password.
*
* @param string $secret - Secret key in binary form.
* @param int $counter - Timestamp as returned by getTimestamp.
*
* @throws SecretKeyTooShortException
*
* @return string
*/
public function oathHotp($secret, $counter)
{
$secret = $this->base32Decode($this->getSecret($secret));
if (strlen($secret) < 8) {
throw new SecretKeyTooShortException();
}
// Counter must be 64-bit int
$bin_counter = pack('N*', 0, $counter);
$hash = hash_hmac('sha1', $bin_counter, $secret, true);
return str_pad($this->oathTruncate($hash), $this->getOneTimePasswordLength(), '0', STR_PAD_LEFT);
}
/**
* Extracts the OTP from the SHA1 hash.
*
* @param string $hash
*
* @return int
**/
public function oathTruncate($hash)
{
$offset = ord($hash[19]) & 0xf;
$temp = unpack('N', substr($hash, $offset, 4));
return substr($temp[1] & 0x7fffffff, -$this->getOneTimePasswordLength());
}
/**
* Remove invalid chars from a base 32 string.
*
* @param $string
*
* @return mixed
*/
public function removeInvalidChars($string)
{
return preg_replace('/[^'.Constants::VALID_FOR_B32.']/', '', $string);
}
/**
* Setter for the enforce Google Authenticator compatibility property.
*
* @param mixed $enforceGoogleAuthenticatorCompatibility
*
* @return $this
*/
public function setEnforceGoogleAuthenticatorCompatibility($enforceGoogleAuthenticatorCompatibility)
{
$this->enforceGoogleAuthenticatorCompatibility = $enforceGoogleAuthenticatorCompatibility;
return $this;
}
/**
* Set key regeneration.
*
* @param mixed $keyRegeneration
*/
public function setKeyRegeneration($keyRegeneration)
{
$this->keyRegeneration = $keyRegeneration;
}
/**
* Set OTP length.
*
* @param mixed $oneTimePasswordLength
*/
public function setOneTimePasswordLength($oneTimePasswordLength)
{
$this->oneTimePasswordLength = $oneTimePasswordLength;
}
/**
* Set secret.
*
* @param mixed $secret
*/
public function setSecret($secret)
{
$this->secret = $secret;
}
/**
* Set the OTP window.
*
* @param mixed $window
*/
public function setWindow($window)
{
$this->window = $window;
}
/**
* Verifies a user inputted key against the current timestamp. Checks $window
* keys either side of the timestamp.
*
* @param string $key - User specified key
* @param null|string $secret
* @param null|int $window
* @param null|int $timestamp
* @param null|string|int $oldTimestamp
*
* @return bool|int
*/
public function verify($key, $secret = null, $window = null, $timestamp = null, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
{
return $this->verifyKey(
$secret,
$key,
$window,
$timestamp,
$oldTimestamp
);
}
/**
* Verifies a user inputted key against the current timestamp. Checks $window
* keys either side of the timestamp.
*
* @param string $secret
* @param string $key - User specified key
* @param null|int $window
* @param null|int $timestamp
* @param null|string|int $oldTimestamp
*
* @return bool|int
*/
public function verifyKey($secret, $key, $window = null, $timestamp = null, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
{
$timestamp = $this->makeTimestamp($timestamp);
return $this->findValidOTP(
$secret,
$key,
$window,
$this->makeStartingTimestamp($window, $timestamp, $oldTimestamp),
$timestamp,
$oldTimestamp
);
}
/**
* Verifies a user inputted key against the current timestamp. Checks $window
* keys either side of the timestamp, but ensures that the given key is newer than
* the given oldTimestamp. Useful if you need to ensure that a single key cannot
* be used twice.
*
* @param string $secret
* @param string $key - User specified key
* @param int $oldTimestamp - The timestamp from the last verified key
* @param int|null $window
* @param int|null $timestamp
*
* @return bool|int - false (not verified) or the timestamp of the verified key
**/
public function verifyKeyNewer($secret, $key, $oldTimestamp, $window = null, $timestamp = null)
{
return $this->verifyKey($secret, $key, $window, $timestamp, $oldTimestamp);
}
}