diff --git a/src/Base10.php b/src/Base10.php index 1c1d080..a436d64 100644 --- a/src/Base10.php +++ b/src/Base10.php @@ -3,30 +3,38 @@ class Base10{ - public static function decode(string $decimal): string{ - $hex = BaseUtil::str_baseconvert($decimal,10,16); - while(strlen($hex)%2!==0){ - $hex = '0'.$hex; - } - foreach(str_split($decimal) AS $chunk){ - if($chunk!=="0"){ - break; - } - $hex = '00'.$hex; - } - return hex2bin($hex); - } + /** + * @param string $decimal + * @return string + */ + public static function decode(string $decimal): string{ + $hex = BaseUtil::str_baseconvert($decimal,10,16); + while(strlen($hex)%2!==0){ + $hex = '0'.$hex; + } + foreach(str_split($decimal) AS $chunk){ + if($chunk!=="0"){ + break; + } + $hex = '00'.$hex; + } + return hex2bin($hex); + } - public static function encode(string $data): string{ - $hex = bin2hex($data); - $decimal = BaseUtil::str_baseconvert($hex,16,10); - foreach(str_split($data) AS $chunk){ - if($chunk!=="\0"){ - break; - } - $decimal = '0'.$decimal; - } - return $decimal; - } + /** + * @param string $data + * @return string + */ + public static function encode(string $data): string{ + $hex = bin2hex($data); + $decimal = BaseUtil::str_baseconvert($hex,16,10); + foreach(str_split($data) AS $chunk){ + if($chunk!=="\0"){ + break; + } + $decimal = '0'.$decimal; + } + return $decimal; + } } \ No newline at end of file diff --git a/src/Base16.php b/src/Base16.php index 63f1743..7294dc9 100644 --- a/src/Base16.php +++ b/src/Base16.php @@ -3,12 +3,20 @@ class Base16{ - public static function decode(string $hex): string{ - return hex2bin($hex); - } + /** + * @param string $hex + * @return string + */ + public static function decode(string $hex): string{ + return hex2bin($hex); + } - public static function encode(string $data): string{ - return bin2hex($data); - } + /** + * @param string $data + * @return string + */ + public static function encode(string $data): string{ + return bin2hex($data); + } } \ No newline at end of file diff --git a/src/Base2.php b/src/Base2.php index bfcdf62..32e869e 100644 --- a/src/Base2.php +++ b/src/Base2.php @@ -3,25 +3,33 @@ class Base2{ - public static function decode(string $binary): string{ - $data = ''; - foreach(str_split($binary,8) AS $chunk){ - $dataChunk = chr(bindec($chunk)); - $data .= $dataChunk; - } - return $data; - } + /** + * @param string $binary + * @return string + */ + public static function decode(string $binary): string{ + $data = ''; + foreach(str_split($binary,8) AS $chunk){ + $dataChunk = chr(bindec($chunk)); + $data .= $dataChunk; + } + return $data; + } - public static function encode(string $data): string{ - $binary = ''; - foreach(str_split($data) AS $chunk){ - $binaryChunk = decbin(ord($chunk)); - while(strlen($binaryChunk)%8!==0){ - $binaryChunk = '0'.$binaryChunk; - } - $binary .= $binaryChunk; - } - return $binary; - } + /** + * @param string $data + * @return string + */ + public static function encode(string $data): string{ + $binary = ''; + foreach(str_split($data) AS $chunk){ + $binaryChunk = decbin(ord($chunk)); + while(strlen($binaryChunk)%8!==0){ + $binaryChunk = '0'.$binaryChunk; + } + $binary .= $binaryChunk; + } + return $binary; + } } \ No newline at end of file diff --git a/src/Base32.php b/src/Base32.php index 0936d21..1e7bdd8 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -3,87 +3,71 @@ class Base32{ - public static function decode(string $data,string $alphabet): string{ - $flippedAlphabet = array_flip(str_split($alphabet)); - $binary = ''; - for($i=0;$i 8) > $len && 0 == $val) ? '=' : $alphabet[$val >> $shift]; + $val = $val & ((1 << $shift) - 1); + $bitLen -= 5; + } - //Explode string into integers - $chars = (array) unpack('C*', $data, 0); - - while ($n < $len || 0 !== $bitLen) { - //If the bit length has fallen below 5, shift left 8 and add the next character. - if ($bitLen < 5) { - $val = $val << 8; - $bitLen += 8; - $n++; - $val += $chars[$n]; - } - $shift = $bitLen - 5; - $encoded .= ($n - (int)($bitLen > 8) > $len && 0 == $val) ? '=' : $alphabet[$val >> $shift]; - $val = $val & ((1 << $shift) - 1); - $bitLen -= 5; - } - - return strtolower($encoded); - } + return strtolower($encoded); + } } \ No newline at end of file diff --git a/src/Base36.php b/src/Base36.php index c92dd5a..213169c 100644 --- a/src/Base36.php +++ b/src/Base36.php @@ -3,28 +3,36 @@ class Base36{ - public static function decode(string $base36): string{ - $hex = BaseUtil::str_baseconvert(strtolower($base36),36,16); - foreach(str_split($base36) AS $chunk){ - if($chunk!=="0"){ - break; - } - $hex = '00'.$hex; - } - return hex2bin($hex); - } + /** + * @param string $base36 + * @return string + */ + public static function decode(string $base36): string{ + $hex = BaseUtil::str_baseconvert(strtolower($base36),36,16); + foreach(str_split($base36) AS $chunk){ + if($chunk!=="0"){ + break; + } + $hex = '00'.$hex; + } + return hex2bin($hex); + } - public static function encode(string $data): string{ - $hex = bin2hex($data); - $base36 = BaseUtil::str_baseconvert($hex,16,36); - foreach(str_split($data) AS $chunk){ - if($chunk!=="\0"){ - break; - } - $base36 = '0'.$base36; - } + /** + * @param string $data + * @return string + */ + public static function encode(string $data): string{ + $hex = bin2hex($data); + $base36 = BaseUtil::str_baseconvert($hex,16,36); + foreach(str_split($data) AS $chunk){ + if($chunk!=="\0"){ + break; + } + $base36 = '0'.$base36; + } - return $base36; - } + return $base36; + } } \ No newline at end of file diff --git a/src/Base58.php b/src/Base58.php index c1e7ff7..dcf7170 100644 --- a/src/Base58.php +++ b/src/Base58.php @@ -3,74 +3,85 @@ class Base58{ - private static function convertBase(array $source, int $sourceBase, int $targetBase): array{ - $result = []; - while ($count = count($source)) { - $quotient = []; - $remainder = 0; - for ($i = 0; $i !== $count; $i++) { - $accumulator = $source[$i] + $remainder * $sourceBase; - /* Same as PHP 7 intdiv($accumulator, $targetBase) */ - $digit = ($accumulator - ($accumulator % $targetBase)) / $targetBase; - $remainder = $accumulator % $targetBase; - if (count($quotient) || $digit) { - $quotient[] = $digit; - } - } - array_unshift($result, $remainder); - $source = $quotient; - } + /** + * @param array $source + * @param int $sourceBase + * @param int $targetBase + * @return array + */ + private static function convertBase(array $source, int $sourceBase, int $targetBase): array{ + $result = []; + while ($count = count($source)) { + $quotient = []; + $remainder = 0; + for ($i = 0; $i !== $count; $i++) { + $accumulator = $source[$i] + $remainder * $sourceBase; + /* Same as PHP 7 intdiv($accumulator, $targetBase) */ + $digit = ($accumulator - ($accumulator % $targetBase)) / $targetBase; + $remainder = $accumulator % $targetBase; + if (count($quotient) || $digit) { + $quotient[] = $digit; + } + } + array_unshift($result, $remainder); + $source = $quotient; + } - return $result; - } + return $result; + } - public static function decode(string $data,string $alphabet): string{ - $data = str_split($data); - $data = array_map(static function($character) use($alphabet){ - return strpos($alphabet,$character); - }, $data); + /** + * @param string $data + * @param string $alphabet + * @return string + */ + public static function decode(string $data,string $alphabet): string{ + $data = str_split($data); + $data = array_map(static function($character) use($alphabet){ + return strpos($alphabet,$character); + }, $data); - $leadingZeroes = 0; - while (!empty($data) && 0 === $data[0]) { - $leadingZeroes++; - array_shift($data); - } + $leadingZeroes = 0; + while (!empty($data) && 0 === $data[0]) { + $leadingZeroes++; + array_shift($data); + } - $converted = self::convertBase($data, 58, 256); + $converted = self::convertBase($data, 58, 256); - if (0 < $leadingZeroes) { - $converted = array_merge( - array_fill(0, $leadingZeroes, 0), - $converted - ); - } + if (0 < $leadingZeroes) { + $converted = array_merge( + array_fill(0, $leadingZeroes, 0), + $converted + ); + } - $decoded = implode("", array_map("chr", $converted)); - return $decoded; - } + $decoded = implode("", array_map("chr", $converted)); + return $decoded; + } - public static function encode(string $data,string $alphabet): string{ - $data = str_split($data); - $data = array_map("ord", $data); + public static function encode(string $data,string $alphabet): string{ + $data = str_split($data); + $data = array_map("ord", $data); - $leadingZeroes = 0; - while (!empty($data) && 0 === $data[0]) { - $leadingZeroes++; - array_shift($data); - } + $leadingZeroes = 0; + while (!empty($data) && 0 === $data[0]) { + $leadingZeroes++; + array_shift($data); + } - $converted = self::convertBase($data, 256, 58); + $converted = self::convertBase($data, 256, 58); - if (0 < $leadingZeroes) { - $converted = array_merge( - array_fill(0, $leadingZeroes, 0), - $converted - ); - } + if (0 < $leadingZeroes) { + $converted = array_merge( + array_fill(0, $leadingZeroes, 0), + $converted + ); + } - return implode('',array_map(static function($index) use($alphabet){ - return $alphabet[$index]; - },$converted)); - } + return implode('',array_map(static function($index) use($alphabet){ + return $alphabet[$index]; + },$converted)); + } } \ No newline at end of file diff --git a/src/Base64.php b/src/Base64.php index e103514..8078fcf 100644 --- a/src/Base64.php +++ b/src/Base64.php @@ -3,12 +3,20 @@ class Base64{ - public static function decode(string $base64): string{ - return base64_decode($base64); - } + /** + * @param string $base64 + * @return string + */ + public static function decode(string $base64): string{ + return base64_decode($base64); + } - public static function encode(string $data): string{ - return base64_encode($data); - } + /** + * @param string $data + * @return string + */ + public static function encode(string $data): string{ + return base64_encode($data); + } } \ No newline at end of file diff --git a/src/Base8.php b/src/Base8.php index 9c6b52a..c57fa0c 100644 --- a/src/Base8.php +++ b/src/Base8.php @@ -3,33 +3,41 @@ class Base8{ - public static function decode(string $octal): string{ - $binary = ''; - for($i=0;$i= $toBase) { - $nibbles[$newlen++] = (int)($value / $toBase); - $value %= $toBase; - } - else if ($newlen > 0) { - $nibbles[$newlen++] = 0; - } - } - $length = $newlen; - $result = $digits[$value].$result; - } - while ($newlen != 0); - return $result; - } + do { + $value = 0; + $newlen = 0; + for ($i = 0; $i < $length; ++$i) { + $value = $value * $fromBase + $nibbles[$i]; + if ($value >= $toBase) { + $nibbles[$newlen++] = (int)($value / $toBase); + $value %= $toBase; + } + else if ($newlen > 0) { + $nibbles[$newlen++] = 0; + } + } + $length = $newlen; + $result = $digits[$value].$result; + } + while ($newlen != 0); + return $result; + } } \ No newline at end of file