-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTOTP.php
70 lines (59 loc) · 1.32 KB
/
TOTP.php
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
<?php
require_once "HOTP.php";
/**
* This class implements the algorithm outlined in RFC 6238:
* HOTP: Time-Based One-Time Password Algorithm.
*/
class TOTP extends HOTP
{
/** @var int time step (in seconds) */
protected $timeStep = 30;
/** @var int Unix time to start counting time steps */
protected $epoch = 0;
public function __construct($key, $digits = 6)
{
parent::__construct($key, null, $digits);
}
public function getCounter()
{
$currentTime = $this->counter !== null ? $this->counter : self::getCurrentTime();
return (int)floor(($currentTime - $this->epoch) / $this->timeStep);
}
public function increment()
{
if ($this->counter !== null)
$this->counter += $this->timeStep;
}
/**
* @param int $epoch
*/
public function setEpoch($epoch)
{
$this->epoch = $epoch;
}
/**
* @return int
*/
public function getEpoch()
{
return $this->epoch;
}
/**
* @param int $timeStep
*/
public function setTimeStep($timeStep)
{
$this->timeStep = $timeStep;
}
/**
* @return int
*/
public function getTimeStep()
{
return $this->timeStep;
}
public static function getCurrentTime()
{
return time();
}
}