This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
forked from rchouinard/rych-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractOTP.php
245 lines (214 loc) · 6.21 KB
/
AbstractOTP.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
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
<?php
/**
* Ryan's OATH-OTP Library
*
* @package Rych\OTP
* @author Ryan Chouinard <[email protected]>
* @copyright Copyright (c) 2013, Ryan Chouinard
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*/
namespace Rych\OTP;
use Rych\OTP\Seed;
/**
* One-Time Password Base Class
*
* @package Rych\OTP
* @author Ryan Chouinard <[email protected]>
* @copyright Copyright (c) 2013, Ryan Chouinard
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*/
abstract class AbstractOTP
{
/**
* @var integer
*/
protected $digits;
/**
* @var string
*/
protected $hashFunction;
/**
* @var \Rych\OTP\Seed
*/
protected $secret;
/**
* @var integer
*/
protected $window;
/**
* Class constructor
*
* @param string|\Rych\OTP\Seed $secret The shared secret key.
* @param array $options An array of options to be used
* when generating one-time passwords.
* @return void
*/
public function __construct($secret, array $options = array ())
{
// Option names taken from Google Authenticator docs for consistency
$options = array_merge(
array (
'algorithm' => 'sha1',
'digits' => 6,
'window' => 4,
),
array_change_key_case($options, CASE_LOWER)
);
$this->setDigits($options['digits']);
$this->setHashFunction($options['algorithm']);
$this->setSecret($secret);
$this->setWindow($options['window']);
}
/**
* Generate a one-time password from a given counter value
*
* @param integer $counter The counter value. Defaults to 0.
* @return string Returns the generated one-time password.
*/
public function calculate($counter = 0)
{
$digits = $this->getDigits();
$hashFunction = $this->getHashFunction();
$secret = $this->getSecret()->getValue(Seed::FORMAT_RAW);
$counter = $this->counterToString($counter);
$hash = hash_hmac($hashFunction, $counter, $secret, true);
$otp = $this->truncate($hash);
if ($digits < 10) {
$otp %= pow(10, $digits);
}
return str_pad($otp, $digits, '0', STR_PAD_LEFT);
}
/**
* Get the number of digits in the one-time password
*
* @return integer Returns the number of digits in a one-time password.
*/
public function getDigits()
{
return $this->digits;
}
/**
* Set the number of digits in the one-time password
*
* @param integer $digits The number of digits in a
* one-time password.
* @return \Rych\OTP\OTP Returns an instance of self for method
* chaining.
* @throws \InvalidArgumentException Thrown if the requested number of
* digits is outside of the inclusive range 1-10.
*/
public function setDigits($digits)
{
$digits = abs(intval($digits));
if ($digits < 1 || $digits > 10) {
throw new \InvalidArgumentException('Digits must be a number between 1 and 10 inclusive');
}
$this->digits = $digits;
return $this;
}
/**
* Get the hash function
*
* @return string Returns the hash function.
*/
public function getHashFunction()
{
return $this->hashFunction;
}
/**
* Set the hash function
*
* @param string $hashFunction The hash function.
* @return \Rych\OTP\OTP Returns an instance of self for method
* chaining.
* @throws \InvalidArgumentException Thrown if the supplied hash function is
* not supported.
*/
public function setHashFunction($hashFunction)
{
$hashFunction = strtolower($hashFunction);
if (!in_array($hashFunction, hash_algos())) {
throw new \InvalidArgumentException("$hashFunction is not a supported hash function");
}
$this->hashFunction = $hashFunction;
return $this;
}
/**
* Get the shared secret key
*
* @return \Rych\OTP\Seed Returns a Seed object instance which represents
* the shared secret key.
*/
public function getSecret()
{
return $this->secret;
}
/**
* Set the shared secret key
*
* @param string|\Rych\OTP\Seed $secret The shared secret key.
* @return \Rych\OTP\OTP Returns an instance of self for method
* chaining.
*/
public function setSecret($secret)
{
if (!$secret instanceof Seed) {
$secret = new Seed($secret);
}
$this->secret = $secret;
return $this;
}
/**
* Get the window value
*
* @return integer The window value.
*/
public function getWindow()
{
return $this->window;
}
/**
* Set the window value
*
* @param integer $window The window value
* @return \Rych\OTP\HOTP Returns an instance of self for method chaining.
*/
public function setWindow($window)
{
$window = abs(intval($window));
$this->window = $window;
return $this;
}
/**
* Extract 4 bytes from a hash value
*
* Uses the method defined in RFC 4226, § 5.4.
*
* @param string $hash Hash value.
* @return integer Truncated hash value.
*/
private function truncate($hash)
{
$offset = ord($hash[19]) & 0xf;
$value = (ord($hash[$offset + 0]) & 0x7f) << 24;
$value |= (ord($hash[$offset + 1]) & 0xff) << 16;
$value |= (ord($hash[$offset + 2]) & 0xff) << 8;
$value |= (ord($hash[$offset + 3]) & 0xff);
return $value;
}
/**
* Convert an integer counter into a string of 8 bytes
*
* @param integer $counter The counter value.
* @return string Returns an 8-byte binary string.
*/
private function counterToString($counter)
{
$temp = array ();
while ($counter != 0) {
$temp[] = chr($counter & 0xff);
$counter >>= 8;
}
return str_pad(join(array_reverse($temp)), 8, "\0", STR_PAD_LEFT);
}
}