-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.php
executable file
·78 lines (70 loc) · 1.52 KB
/
Test.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
#!/usr/bin/php -q
<?php
require_once('PHPOTP.php');
require_once('Base32.php');
function DumpBinStr($BinStr) {
$Result='';
$Len=strlen($BinStr);
for ($i=0; $i<$Len; $i++) {
$Value=ord($BinStr[$i]);
if ($Value<16)
$Result.='0';
$Result.=dechex($Value).' ';
}
return rtrim($Result,' ');
}
/* Straight from RFC 4648 Test Vectors */
$Base32KnownGood=array(
''=>'',
'f'=>'MY======',
'fo'=>'MZXQ====',
'foo'=>'MZXW6===',
'foob'=>'MZXW6YQ=',
'fooba'=>'MZXW6YTB',
'foobar'=>'MZXW6YTBOI======'
);
echo "\nBase32::Encode Test Vectors\n";
foreach ($Base32KnownGood as $Key=>$Value) {
echo "'".$Key."' -> '".$Value."': ";
$Result=Base32::Encode($Key);
if ($Result===$Value) {
echo 'PASS';
} else {
echo "FAIL ('".$Result."')";
}
echo "\n";
}
echo "\nBase32::Decode Test Vectors\n";
foreach ($Base32KnownGood as $Key=>$Value) {
echo "'".$Value."' -> '".$Key."': ";
$Result=Base32::Decode($Value);
if ($Result===$Key) {
echo 'PASS';
} else {
echo "FAIL ('".$Key."')";
}
echo "\n";
}
echo "\nBase32 Encode/Decode Excercise";
$Passes=100;
$MaxLen=100;
$Failures=0;
mt_srand();
for ($i=0; $i<$Passes; $i++) {
$RandStr='';
$Len=mt_rand(0,$MaxLen);
for ($Leni=0; $Leni<$Len; $Leni++) {
$RandStr.=chr(mt_rand(0,255));
}
$Encoded=Base32::Encode($RandStr);
$Decoded=Base32::Decode($Encoded);
if ($Decoded!=$RandStr) {
echo "\nFAIL\n";
echo "Input:\t\t ".DumpBinStr($RandStr)."\n";
echo "Encoded:\t ".$Encoded."\n";
echo "Decoded:\t ".DumpBinStr($Decoded)."\n";
$Failures++;
}
}
if ($Failures==0)
echo ": PASS\n";