|
@@ -1,8 +1,10 @@
|
|
|
<?php
|
|
|
|
|
|
+namespace OTPHP;
|
|
|
+
|
|
|
/**
|
|
|
* Encode in Base32 based on RFC 4648.
|
|
|
- * Requires 20% more space than base64
|
|
|
+ * Requires 20% more space than base64
|
|
|
* Great for case-insensitive filesystems like Windows and URL's (except for = char which can be excluded using the pad option for urls)
|
|
|
*
|
|
|
* @package default
|
|
@@ -17,14 +19,14 @@ class Base32 {
|
|
|
'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
|
|
|
'=' // padding char
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
private static $flippedMap = array(
|
|
|
'A'=>'0', 'B'=>'1', 'C'=>'2', 'D'=>'3', 'E'=>'4', 'F'=>'5', 'G'=>'6', 'H'=>'7',
|
|
|
'I'=>'8', 'J'=>'9', 'K'=>'10', 'L'=>'11', 'M'=>'12', 'N'=>'13', 'O'=>'14', 'P'=>'15',
|
|
|
'Q'=>'16', 'R'=>'17', 'S'=>'18', 'T'=>'19', 'U'=>'20', 'V'=>'21', 'W'=>'22', 'X'=>'23',
|
|
|
'Y'=>'24', 'Z'=>'25', '2'=>'26', '3'=>'27', '4'=>'28', '5'=>'29', '6'=>'30', '7'=>'31'
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Use padding false when encoding for urls
|
|
|
*
|
|
@@ -41,7 +43,7 @@ class Base32 {
|
|
|
$fiveBitBinaryArray = str_split($binaryString, 5);
|
|
|
$base32 = "";
|
|
|
$i=0;
|
|
|
- while($i < count($fiveBitBinaryArray)) {
|
|
|
+ while($i < count($fiveBitBinaryArray)) {
|
|
|
$base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)];
|
|
|
$i++;
|
|
|
}
|
|
@@ -53,14 +55,14 @@ class Base32 {
|
|
|
}
|
|
|
return $base32;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static function decode($input) {
|
|
|
if(empty($input)) return;
|
|
|
$paddingCharCount = substr_count($input, self::$map[32]);
|
|
|
$allowedValues = array(6,4,3,1,0);
|
|
|
if(!in_array($paddingCharCount, $allowedValues)) return false;
|
|
|
- for($i=0; $i<4; $i++){
|
|
|
- if($paddingCharCount == $allowedValues[$i] &&
|
|
|
+ for($i=0; $i<4; $i++){
|
|
|
+ if($paddingCharCount == $allowedValues[$i] &&
|
|
|
substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])) return false;
|
|
|
}
|
|
|
$input = str_replace('=','', $input);
|