-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase32.php
More file actions
141 lines (121 loc) · 4.21 KB
/
Base32.php
File metadata and controls
141 lines (121 loc) · 4.21 KB
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
<?php
/**
* NTICompass' crappy base32 library for PHP
*
* http://labs.nticompassinc.com
*/
class Base32 {
var $encode, $decode, $type;
// Supports RFC 4648 (default) or Crockford (http://www.crockford.com/wrmg/base32.html)
function __construct($alphabet = 'rfc4648')
{
$alphabet = strtolower($alphabet);
$this->type = $alphabet;
// Crockford's alphabet removes I,L,O, and U
$crockfordABC = range('A', 'Z');
unset($crockfordABC[8], $crockfordABC[11], $crockfordABC[14], $crockfordABC[20]);
$crockfordABC = array_values($crockfordABC);
$alphabets = array(
'rfc4648' => array_merge(range('A', 'Z'), range(2, 7), array('=')),
'crockford' => array_merge(range(0, 9), $crockfordABC, array('='))
);
$this->encode = $alphabets[$alphabet];
$this->decode = array_flip($this->encode);
// Add extra letters for Crockford's alphabet
if ($alphabet === 'crockford') {
$this->decode['O'] = 0;
$this->decode['I'] = 1;
$this->decode['L'] = 1;
}
}
private function bin_chunk($binaryString, $bits)
{
$binaryString = chunk_split($binaryString, $bits, ' ');
if ($this->endsWith($binaryString, ' ')) {
$binaryString = substr($binaryString, 0, strlen($binaryString) - 1);
}
return explode(' ', $binaryString);
}
// String <-> Binary conversion
// Based off: http://psoug.org/snippet/PHP-Binary-to-Text-Text-to-Binary_380.htm
private function bin2str($binaryString)
{
// Make sure binary string is in 8-bit chunks
$binaryArray = $this->bin_chunk($binaryString, 8);
$string = '';
foreach ($binaryArray as $bin) {
// Pad each value to 8 bits
$bin = str_pad($bin, 8, 0, STR_PAD_RIGHT);
// Convert binary strings to ascii
$string .= chr(bindec($bin));
}
return $string;
}
private function str2bin($input)
{
$bin = '';
foreach (str_split($input) as $s) {
// Return each character as an 8-bit binary string
$s = decbin(ord($s));
$bin .= str_pad($s, 8, 0, STR_PAD_LEFT);
}
return $bin;
}
// starts/endsWith from:
// http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355
private function startsWith($haystack, $needle)
{
$length = strlen($needle);
return substr($haystack, 0, $length) === $needle;
}
private function endsWith($haystack, $needle)
{
$length = strlen($needle);
return substr($haystack, -$length) === $needle;
}
// base32 info from: http://www.garykessler.net/library/base64.html
// base32_encode
public function base32_encode($string)
{
// Convert string to binary
$binaryString = $this->str2bin($string);
// Break into 5-bit chunks, then break that into an array
$binaryArray = $this->bin_chunk($binaryString, 5);
// Pad array to be divisible by 8
while (count($binaryArray) % 8 !== 0) {
$binaryArray[] = null;
}
$base32String = '';
// Encode in base32
foreach ($binaryArray as $bin) {
$char = 32;
if (!is_null($bin)) {
// Pad the binary strings
$bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
$char = bindec($bin);
}
// Base32 character
$base32String .= $this->encode[$char];
}
return $base32String;
}
// base32_decode
public function base32_decode($base32String)
{
$base32Array = str_split(str_replace('-', '', strtoupper($base32String)));
$binaryArray = array();
$string = '';
foreach ($base32Array as $str) {
$char = $this->decode[$str];
if ($char !== 32) {
$char = decbin($char);
$string .= str_pad($char, 5, 0, STR_PAD_LEFT);
}
}
while (strlen($string) % 8 !== 0) {
$string = substr($string, 0, strlen($string) - 1);
}
return $this->bin2str($string);
}
}
?>