1 | <?php |
||
16 | class OTPAuthenticate |
||
17 | { |
||
18 | /** int verification code modulus */ |
||
19 | const VERIFICATION_CODE_MODULUS = 1e6; |
||
20 | |||
21 | /** int Secret length */ |
||
22 | protected $secret_length; |
||
23 | |||
24 | /** int code length */ |
||
25 | protected $code_length; |
||
26 | |||
27 | /** \Base32\Base32 */ |
||
28 | protected $base32; |
||
29 | |||
30 | /** |
||
31 | * Constructor for OTPAuthenticate |
||
32 | * |
||
33 | * @param int $code_length Code length |
||
34 | * @param int $secret_length Secret length |
||
35 | */ |
||
36 | 16 | public function __construct($code_length = 6, $secret_length = 10) |
|
43 | |||
44 | /** |
||
45 | * Generates code based on timestamp and secret |
||
46 | * |
||
47 | * @param string $secret Secret shared with user |
||
48 | * @param int $counter Counter for code generation |
||
49 | * @param string $algorithm Algorithm to use for HMAC hash. |
||
50 | * Defaults to sha512. The following hash types are allowed: |
||
51 | * TOTP: sha1, sha256, sha512 |
||
52 | * HOTP: sha1 |
||
53 | * |
||
54 | * @return string Generated OTP code |
||
55 | */ |
||
56 | 14 | public function generateCode($secret, $counter, $algorithm = 'sha512') |
|
69 | |||
70 | /** |
||
71 | * Check if supplied TOTP code is valid |
||
72 | * |
||
73 | * @param string $secret Secret to use for comparison |
||
74 | * @param int $code Supplied TOTP code |
||
75 | * @param string $hash_type Hash type |
||
76 | * |
||
77 | * @return bool True if code is valid, false if not |
||
78 | */ |
||
79 | 5 | public function checkTOTP($secret, $code, $hash_type = 'sha512') |
|
93 | |||
94 | /** |
||
95 | * Check if supplied HOTP code is valid |
||
96 | * |
||
97 | * @param string $secret Secret to use for comparison |
||
98 | * @param int $counter Current counter |
||
99 | * @param int $code Supplied HOTP code |
||
100 | * @param string $hash_type Hash type |
||
101 | * |
||
102 | * @return bool True if code is valid, false if not |
||
103 | */ |
||
104 | 5 | public function checkHOTP($secret, $counter, $code, $hash_type = 'sha512') |
|
108 | |||
109 | /** |
||
110 | * Truncate HMAC hash to binary for generating a TOTP code |
||
111 | * |
||
112 | * @param string $hash HMAC hash |
||
113 | * |
||
114 | * @return int Truncated binary hash |
||
115 | */ |
||
116 | 13 | protected function truncate($hash) |
|
134 | |||
135 | /** |
||
136 | * Get binary version of time counter |
||
137 | * |
||
138 | * @param int $counter Timestamp or counter |
||
139 | * |
||
140 | * @return string Binary time counter |
||
141 | */ |
||
142 | 13 | protected function getBinaryCounter($counter) |
|
146 | |||
147 | /** |
||
148 | * Get counter from timestamp |
||
149 | * |
||
150 | * @param int $time Timestamp |
||
151 | * |
||
152 | * @return int Counter |
||
153 | */ |
||
154 | 7 | public function getTimestampCounter($time) |
|
158 | |||
159 | /** |
||
160 | * Generate secret with specified length |
||
161 | * |
||
162 | * @param int $length |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 2 | public function generateSecret($length = 10) |
|
183 | } |
||
184 |