-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase32.php
77 lines (65 loc) · 1.73 KB
/
base32.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
<?php
function base32_encode($string)
{
static $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
$length = strlen($string);
$result = "";
$buffer = ord($string[0]);
$next = 1;
$bitsLeft = 8;
while ($bitsLeft > 0 || $next < $length)
{
if ($bitsLeft < 5)
{
if ($next < $length)
{
$buffer <<= 8;
$buffer |= ord($string[$next++]);
$bitsLeft += 8;
}
else
{
$pad = 5 - $bitsLeft;
$buffer <<= $pad;
$bitsLeft += $pad;
}
}
$index = 0x1F & ($buffer >> ($bitsLeft - 5));
$bitsLeft -= 5;
$result .= $charset[$index];
}
return $result;
}
function base32_decode($string)
{
$length = strlen($string);
$buffer = 0;
$bitsLeft = 0;
$result = "";
for ($i = 0; $i < $length; $i += 1)
{
$ch = $string[$i];
/* Skip whitespace. */
if ($ch == " " || $ch == "\t" || $ch == "\r" || $ch == "\n" || $ch == "-")
continue;
/* Deal with commonly mistyped characters. */
if ($ch == "0") $ch = "O";
if ($ch == "1") $ch = "L";
if ($ch == "8") $ch = "B";
/* Look up the value of this digit. */
if (($ch >= "A" && $ch <= "Z") || ($ch >= "a" && $ch <= "z"))
$value = (ord($ch) & 0x1F) - 1;
else if ($ch >= "2" && $ch <= "7")
$value = ord($ch) - 24;
else
return null;
$buffer |= $value;
$bitsLeft += 5;
if ($bitsLeft >= 8)
{
$result .= chr($buffer >> ($bitsLeft - 8));
$bitsLeft -= 8;
}
}
return $result;
}