-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.php
161 lines (144 loc) · 4.37 KB
/
example.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
<?php
declare(strict_types=1);
/**
* HOTP Example File
*/
require_once 'src/HOTP.php';
use jakobo\HOTP\HOTP;
$key = '12345678901234567890';
$table = [
'HOTP' => [
[
'HMAC' => 'cc93cf18508d94934c64b65d8ba7667fb7cde4b0',
'hex' => '4c93cf18',
'dec' => '1284755224',
'hotp' => '755224',
],
[
'HMAC' => '75a48a19d4cbe100644e8ac1397eea747a2d33ab',
'hex' => '41397eea',
'dec' => '1094287082',
'hotp' => '287082',
],
[
'HMAC' => '0bacb7fa082fef30782211938bc1c5e70416ff44',
'hex' => '82fef30',
'dec' => '137359152',
'hotp' => '359152',
],
[
'HMAC' => '66c28227d03a2d5529262ff016a1e6ef76557ece',
'hex' => '66ef7655',
'dec' => '1726969429',
'hotp' => '969429',
],
[
'HMAC' => 'a904c900a64b35909874b33e61c5938a8e15ed1c',
'hex' => '61c5938a',
'dec' => '1640338314',
'hotp' => '338314',
],
[
'HMAC' => 'a37e783d7b7233c083d4f62926c7a25f238d0316',
'hex' => '33c083d4',
'dec' => '868254676',
'hotp' => '254676',
],
[
'HMAC' => 'bc9cd28561042c83f219324d3c607256c03272ae',
'hex' => '7256c032',
'dec' => '1918287922',
'hotp' => '287922',
],
[
'HMAC' => 'a4fb960c0bc06e1eabb804e5b397cdc4b45596fa',
'hex' => '4e5b397',
'dec' => '82162583',
'hotp' => '162583',
],
[
'HMAC' => '1b3c89f65e6c9e883012052823443f048b4332db',
'hex' => '2823443f',
'dec' => '673399871',
'hotp' => '399871',
],
[
'HMAC' => '1637409809a679dc698207310c8c7fc07290d9e5',
'hex' => '2679dc69',
'dec' => '645520489',
'hotp' => '520489',
],
],
'TOTP' => [
'59' => [
'totp' => '94287082',
],
'1111111109' => [
'totp' => '07081804',
],
'1111111111' => [
'totp' => '14050471',
],
'1234567890' => [
'totp' => '89005924',
],
'2000000000' => [
'totp' => '69279037',
],
],
];
echo <<<DOCBLOCK
<!DOCTYPE><html><head></head><body><pre>
http://www.ietf.org/rfc/rfc4226.txt
http://tools.ietf.org/html/draft-mraihi-totp-timebased-06
TEST VECTOR VERIFICATION
HOTP Tests:
DOCBLOCK;
echo "Count Method Value Pass/Fail\n";
echo "----------------------------------------------------------------------\n";
// loop over all HOTP table results, and calculate the matching value
foreach ($table['HOTP'] as $seed => $results) {
$hotp = HOTP::generateByCounter($key, $seed);
$first = true;
foreach ($results as $type => $calc) {
if ($first) {
echo str_pad($seed, 4, ' ', STR_PAD_LEFT);
$first = false;
} else {
echo ' ';
}
echo ' ';
echo str_pad($type, 5, ' ', STR_PAD_RIGHT);
echo ' ';
echo str_pad($calc, 47, ' ', STR_PAD_RIGHT);
echo ' ';
$method = 'to' . (ucfirst(str_replace('HMAC', 'string', $type)));
echo str_pad(($calc == $hotp->$method(6)) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT);
echo "\n";
}
}
echo <<<DOCBLOCK
TOTP Tests:
DOCBLOCK;
echo "Time (sec) Value Pass/Fail\n";
echo "----------------------------------------------------------------------\n";
// now echo over the TOTP table
foreach ($table['TOTP'] as $seed => $results) {
$totp = HOTP::generateByTime($key, 30, $seed);
$first = true;
foreach ($results as $type => $calc) {
if ($first) {
echo str_pad($seed, 10, ' ', STR_PAD_LEFT);
$first = false;
} else {
echo ' ';
}
echo ' ';
echo str_pad($calc, 47, ' ', STR_PAD_RIGHT);
echo ' ';
$method = 'to' . (ucfirst(str_replace('totp', 'hotp', $type)));
echo str_pad(($calc == $totp->$method(8)) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT);
echo "\n";
}
}
echo '</pre></body></html>';