]> git.wh0rd.org - tt-rss.git/blob - vendor/OTPHP/Base32.php
move OTPHP to vendor/; additionally move Base32 class to OTPHP namespace
[tt-rss.git] / vendor / OTPHP / Base32.php
1 <?php
2
3 namespace OTPHP;
4
5 /**
6 * Encode in Base32 based on RFC 4648.
7 * Requires 20% more space than base64
8 * Great for case-insensitive filesystems like Windows and URL's (except for = char which can be excluded using the pad option for urls)
9 *
10 * @package default
11 * @author Bryan Ruiz
12 **/
13 class Base32 {
14
15 private static $map = array(
16 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
17 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
18 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
19 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
20 '=' // padding char
21 );
22
23 private static $flippedMap = array(
24 'A'=>'0', 'B'=>'1', 'C'=>'2', 'D'=>'3', 'E'=>'4', 'F'=>'5', 'G'=>'6', 'H'=>'7',
25 'I'=>'8', 'J'=>'9', 'K'=>'10', 'L'=>'11', 'M'=>'12', 'N'=>'13', 'O'=>'14', 'P'=>'15',
26 'Q'=>'16', 'R'=>'17', 'S'=>'18', 'T'=>'19', 'U'=>'20', 'V'=>'21', 'W'=>'22', 'X'=>'23',
27 'Y'=>'24', 'Z'=>'25', '2'=>'26', '3'=>'27', '4'=>'28', '5'=>'29', '6'=>'30', '7'=>'31'
28 );
29
30 /**
31 * Use padding false when encoding for urls
32 *
33 * @return base32 encoded string
34 * @author Bryan Ruiz
35 **/
36 public static function encode($input, $padding = true) {
37 if(empty($input)) return "";
38 $input = str_split($input);
39 $binaryString = "";
40 for($i = 0; $i < count($input); $i++) {
41 $binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
42 }
43 $fiveBitBinaryArray = str_split($binaryString, 5);
44 $base32 = "";
45 $i=0;
46 while($i < count($fiveBitBinaryArray)) {
47 $base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)];
48 $i++;
49 }
50 if($padding && ($x = strlen($binaryString) % 40) != 0) {
51 if($x == 8) $base32 .= str_repeat(self::$map[32], 6);
52 else if($x == 16) $base32 .= str_repeat(self::$map[32], 4);
53 else if($x == 24) $base32 .= str_repeat(self::$map[32], 3);
54 else if($x == 32) $base32 .= self::$map[32];
55 }
56 return $base32;
57 }
58
59 public static function decode($input) {
60 if(empty($input)) return;
61 $paddingCharCount = substr_count($input, self::$map[32]);
62 $allowedValues = array(6,4,3,1,0);
63 if(!in_array($paddingCharCount, $allowedValues)) return false;
64 for($i=0; $i<4; $i++){
65 if($paddingCharCount == $allowedValues[$i] &&
66 substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])) return false;
67 }
68 $input = str_replace('=','', $input);
69 $input = str_split($input);
70 $binaryString = "";
71 for($i=0; $i < count($input); $i = $i+8) {
72 $x = "";
73 if(!in_array($input[$i], self::$map)) return false;
74 for($j=0; $j < 8; $j++) {
75 $x .= str_pad(base_convert(@self::$flippedMap[@$input[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
76 }
77 $eightBits = str_split($x, 8);
78 for($z = 0; $z < count($eightBits); $z++) {
79 $binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:"";
80 }
81 }
82 return $binaryString;
83 }
84 }
85