diff options
author | Paragon Initiative Enterprises <security@paragonie.com> | 2016-04-08 10:22:38 -0400 |
---|---|---|
committer | Scott <scott@paragonie.com> | 2016-04-08 10:22:38 -0400 |
commit | 5f792b8d90c966da6e4ffdee6752cb718f6e7e96 (patch) | |
tree | 5a9505c91e10bb120333d50561fd2f310756ad39 /src | |
parent | 75ad93871f1043745a6da8c013b2e73a685b3f30 (diff) | |
download | constant_time_encoding-5f792b8d90c966da6e4ffdee6752cb718f6e7e96.zip constant_time_encoding-5f792b8d90c966da6e4ffdee6752cb718f6e7e96.tar.gz constant_time_encoding-5f792b8d90c966da6e4ffdee6752cb718f6e7e96.tar.bz2 |
D.R.Y.
Diffstat (limited to 'src')
-rw-r--r-- | src/Base32.php | 479 | ||||
-rw-r--r-- | src/Encoding.php | 110 |
2 files changed, 248 insertions, 341 deletions
diff --git a/src/Base32.php b/src/Base32.php index 629c568..a1d7d79 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -17,150 +17,118 @@ abstract class Base32 implements EncoderInterface */ public static function decode($src) { - // Remove padding - $srcLen = Binary::safeStrlen($src); - if ($srcLen === 0) { - return ''; - } - if (($srcLen & 7) === 0) { - for ($j = 0; $j < 7; ++$j) { - if ($src[$srcLen - 1] === '=') { - $srcLen--; - } else { - break; - } - } - } - if (($srcLen & 7) === 1) { - throw new \RangeException( - 'Incorrect padding' - ); - } + return static::doDecode($src, false); + } - $err = 0; - $dest = ''; - for ($i = 0; $i + 8 <= $srcLen; $i += 8) { - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8)); - $c0 = static::decode5Bits($chunk[1]); - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); - $c3 = static::decode5Bits($chunk[4]); - $c4 = static::decode5Bits($chunk[5]); - $c5 = static::decode5Bits($chunk[6]); - $c6 = static::decode5Bits($chunk[7]); - $c7 = static::decode5Bits($chunk[8]); + /** + * Decode a Base32-encoded string into raw binary + * + * @param string $src + * @return string + */ + public static function decodeUpper($src) + { + return static::doDecode($src, true); + } - $dest .= \pack( - 'CCCCC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff, - (($c3 << 4) | ($c4 >> 1) ) & 0xff, - (($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff, - (($c6 << 5) | ($c7 ) ) & 0xff - ); - $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6 | $c7) >> 8; - } - if ($i < $srcLen) { - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); - $c0 = static::decode5Bits($chunk[1]); + /** + * Encode into Base32 (RFC 4648) + * + * @param string $src + * @return string + */ + public static function encode($src) + { + return static::doEncode($src, false); + } - if ($i + 6 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); - $c3 = static::decode5Bits($chunk[4]); - $c4 = static::decode5Bits($chunk[5]); - $c5 = static::decode5Bits($chunk[6]); - $c6 = static::decode5Bits($chunk[7]); + /** + * Encode into Base32 (RFC 4648) + * + * @param string $src + * @return string + */ + public static function encodeUpper($src) + { + return static::doEncode($src, true); + } - $dest .= \pack( - 'CCCC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff, - (($c3 << 4) | ($c4 >> 1) ) & 0xff, - (($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff - ); - $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8; - } elseif ($i + 5 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); - $c3 = static::decode5Bits($chunk[4]); - $c4 = static::decode5Bits($chunk[5]); - $c5 = static::decode5Bits($chunk[6]); + /** + * + * @param int $src + * @return int + */ + protected static function decode5Bits($src) + { + $ret = -1; - $dest .= \pack( - 'CCCC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff, - (($c3 << 4) | ($c4 >> 1) ) & 0xff, - (($c4 << 7) | ($c5 << 2) ) & 0xff - ); - $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5) >> 8; - } elseif ($i + 4 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); - $c3 = static::decode5Bits($chunk[4]); - $c4 = static::decode5Bits($chunk[5]); + // if ($src > 96 && $src < 123) $ret += $src - 97 + 1; // -64 + $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 96); - $dest .= \pack( - 'CCC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff, - (($c3 << 4) | ($c4 >> 1) ) & 0xff - ); - $err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8; - } elseif ($i + 3 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); - $c3 = static::decode5Bits($chunk[4]); + // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 + $ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23); - $dest .= \pack( - 'CC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff - ); - $err |= ($c0 | $c1 | $c2 | $c3) >> 8; - } elseif ($i + 2 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); - $c2 = static::decode5Bits($chunk[3]); + return $ret; + } - $dest .= \pack( - 'CC', - (($c0 << 3) | ($c1 >> 2) ) & 0xff, - (($c1 << 6) | ($c2 << 1) ) & 0xff - ); - $err |= ($c0 | $c1 | $c2) >> 8; - } elseif ($i + 1 < $srcLen) { - $c1 = static::decode5Bits($chunk[2]); + /** + * + * @param int $src + * @return int + */ + protected static function decode5BitsUpper($src) + { + $ret = -1; - $dest .= \pack( - 'C', - (($c0 << 3) | ($c1 >> 2) ) & 0xff - ); - $err |= ($c0 | $c1) >> 8; - } else { - $dest .= \pack( - 'C', - (($c0 << 3) ) & 0xff - ); - $err |= ($c0) >> 8; - } - } - if ($err !== 0) { - throw new \RangeException( - 'base32Decode() only expects characters in the correct base32 alphabet' - ); - } - return $dest; + // if ($src > 64 && $src < 91) $ret += $src - 65 + 1; // -64 + $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); + + // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 + $ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23); + + return $ret; } /** - * Decode a Base32-encoded string into raw binary - * - * @param string $src + * @param $src * @return string */ - public static function decodeUpper($src) + protected static function encode5Bits($src) + { + $diff = 0x61; + + // if ($src > 25) $ret -= 72; + $diff -= ((25 - $src) >> 8) & 73; + + return \pack('C', $src + $diff); + } + + /** + * @param $src + * @return string + */ + protected static function encode5BitsUpper($src) + { + $diff = 0x41; + + // if ($src > 25) $ret -= 40; + $diff -= ((25 - $src) >> 8) & 41; + + return \pack('C', $src + $diff); + } + + + /** + * @param $src + * @param bool $upper + * @return string + */ + protected static function doDecode($src, $upper = false) { + $method = $upper + ? 'decode5BitsUpper' + : 'decode5Bits'; + // Remove padding $srcLen = Binary::safeStrlen($src); if ($srcLen === 0) { @@ -185,14 +153,14 @@ abstract class Base32 implements EncoderInterface $dest = ''; for ($i = 0; $i + 8 <= $srcLen; $i += 8) { $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8)); - $c0 = static::decode5BitsUpper($chunk[1]); - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); - $c3 = static::decode5BitsUpper($chunk[4]); - $c4 = static::decode5BitsUpper($chunk[5]); - $c5 = static::decode5BitsUpper($chunk[6]); - $c6 = static::decode5BitsUpper($chunk[7]); - $c7 = static::decode5BitsUpper($chunk[8]); + $c0 = static::$method($chunk[1]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); + $c3 = static::$method($chunk[4]); + $c4 = static::$method($chunk[5]); + $c5 = static::$method($chunk[6]); + $c6 = static::$method($chunk[7]); + $c7 = static::$method($chunk[8]); $dest .= \pack( 'CCCCC', @@ -206,15 +174,15 @@ abstract class Base32 implements EncoderInterface } if ($i < $srcLen) { $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); - $c0 = static::decode5BitsUpper($chunk[1]); + $c0 = static::$method($chunk[1]); if ($i + 6 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); - $c3 = static::decode5BitsUpper($chunk[4]); - $c4 = static::decode5BitsUpper($chunk[5]); - $c5 = static::decode5BitsUpper($chunk[6]); - $c6 = static::decode5BitsUpper($chunk[7]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); + $c3 = static::$method($chunk[4]); + $c4 = static::$method($chunk[5]); + $c5 = static::$method($chunk[6]); + $c6 = static::$method($chunk[7]); $dest .= \pack( 'CCCC', @@ -225,11 +193,11 @@ abstract class Base32 implements EncoderInterface ); $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8; } elseif ($i + 5 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); - $c3 = static::decode5BitsUpper($chunk[4]); - $c4 = static::decode5BitsUpper($chunk[5]); - $c5 = static::decode5BitsUpper($chunk[6]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); + $c3 = static::$method($chunk[4]); + $c4 = static::$method($chunk[5]); + $c5 = static::$method($chunk[6]); $dest .= \pack( 'CCCC', @@ -240,10 +208,10 @@ abstract class Base32 implements EncoderInterface ); $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5) >> 8; } elseif ($i + 4 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); - $c3 = static::decode5BitsUpper($chunk[4]); - $c4 = static::decode5BitsUpper($chunk[5]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); + $c3 = static::$method($chunk[4]); + $c4 = static::$method($chunk[5]); $dest .= \pack( 'CCC', @@ -253,9 +221,9 @@ abstract class Base32 implements EncoderInterface ); $err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8; } elseif ($i + 3 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); - $c3 = static::decode5BitsUpper($chunk[4]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); + $c3 = static::$method($chunk[4]); $dest .= \pack( 'CC', @@ -264,8 +232,8 @@ abstract class Base32 implements EncoderInterface ); $err |= ($c0 | $c1 | $c2 | $c3) >> 8; } elseif ($i + 2 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); - $c2 = static::decode5BitsUpper($chunk[3]); + $c1 = static::$method($chunk[2]); + $c2 = static::$method($chunk[3]); $dest .= \pack( 'CC', @@ -274,7 +242,7 @@ abstract class Base32 implements EncoderInterface ); $err |= ($c0 | $c1 | $c2) >> 8; } elseif ($i + 1 < $srcLen) { - $c1 = static::decode5BitsUpper($chunk[2]); + $c1 = static::$method($chunk[2]); $dest .= \pack( 'C', @@ -303,79 +271,12 @@ abstract class Base32 implements EncoderInterface * @param string $src * @return string */ - public static function encode($src) - { - $dest = ''; - $srcLen = Binary::safeStrlen($src); - for ($i = 0; $i + 5 <= $srcLen; $i += 5) { - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 5)); - $b0 = $chunk[1]; - $b1 = $chunk[2]; - $b2 = $chunk[3]; - $b3 = $chunk[4]; - $b4 = $chunk[5]; - $dest .= - static::encode5Bits( ($b0 >> 3) & 31) . - static::encode5Bits((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5Bits((($b1 >> 1) ) & 31) . - static::encode5Bits((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5Bits((($b2 << 1) | ($b3 >> 7)) & 31) . - static::encode5Bits((($b3 >> 2) ) & 31) . - static::encode5Bits((($b3 << 3) | ($b4 >> 5)) & 31) . - static::encode5Bits( $b4 & 31); - } - if ($i < $srcLen) { - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); - $b0 = $chunk[1]; - if ($i + 3 < $srcLen) { - $b1 = $chunk[2]; - $b2 = $chunk[3]; - $b3 = $chunk[4]; - $dest .= - static::encode5Bits( ($b0 >> 3) & 31) . - static::encode5Bits((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5Bits((($b1 >> 1) ) & 31) . - static::encode5Bits((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5Bits((($b2 << 1) | ($b3 >> 7)) & 31) . - static::encode5Bits((($b3 >> 2) ) & 31) . - static::encode5Bits((($b3 << 3) ) & 31) . - '='; - } elseif ($i + 2 < $srcLen) { - $b1 = $chunk[2]; - $b2 = $chunk[3]; - $dest .= - static::encode5Bits( ($b0 >> 3) & 31) . - static::encode5Bits((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5Bits((($b1 >> 1) ) & 31) . - static::encode5Bits((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5Bits((($b2 << 1) ) & 31) . - '==='; - } elseif ($i + 1 < $srcLen) { - $b1 = $chunk[2]; - $dest .= - static::encode5Bits( ($b0 >> 3) & 31) . - static::encode5Bits((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5Bits((($b1 >> 1) ) & 31) . - static::encode5Bits((($b1 << 4) ) & 31) . - '===='; - } else { - $dest .= - static::encode5Bits( ($b0 >> 3) & 31) . - static::encode5Bits( ($b0 << 2) & 31) . - '======'; - } - } - return $dest; - } - - /** - * Encode into Base32 (RFC 4648) - * - * @param string $src - * @return string - */ - public static function encodeUpper($src) + protected static function doEncode($src, $upper = false) { + $method = $upper + ? 'encode5BitsUpper' + : 'encode5Bits'; + $dest = ''; $srcLen = Binary::safeStrlen($src); for ($i = 0; $i + 5 <= $srcLen; $i += 5) { @@ -386,14 +287,14 @@ abstract class Base32 implements EncoderInterface $b3 = $chunk[4]; $b4 = $chunk[5]; $dest .= - static::encode5BitsUpper( ($b0 >> 3) & 31) . - static::encode5BitsUpper((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5BitsUpper((($b1 >> 1) ) & 31) . - static::encode5BitsUpper((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5BitsUpper((($b2 << 1) | ($b3 >> 7)) & 31) . - static::encode5BitsUpper((($b3 >> 2) ) & 31) . - static::encode5BitsUpper((($b3 << 3) | ($b4 >> 5)) & 31) . - static::encode5BitsUpper( $b4 & 31); + static::$method( ($b0 >> 3) & 31) . + static::$method((($b0 << 2) | ($b1 >> 6)) & 31) . + static::$method((($b1 >> 1) ) & 31) . + static::$method((($b1 << 4) | ($b2 >> 4)) & 31) . + static::$method((($b2 << 1) | ($b3 >> 7)) & 31) . + static::$method((($b3 >> 2) ) & 31) . + static::$method((($b3 << 3) | ($b4 >> 5)) & 31) . + static::$method( $b4 & 31); } if ($i < $srcLen) { $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); @@ -403,103 +304,39 @@ abstract class Base32 implements EncoderInterface $b2 = $chunk[3]; $b3 = $chunk[4]; $dest .= - static::encode5BitsUpper( ($b0 >> 3) & 31) . - static::encode5BitsUpper((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5BitsUpper((($b1 >> 1) ) & 31) . - static::encode5BitsUpper((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5BitsUpper((($b2 << 1) | ($b3 >> 7)) & 31) . - static::encode5BitsUpper((($b3 >> 2) ) & 31) . - static::encode5BitsUpper((($b3 << 3) ) & 31) . + static::$method( ($b0 >> 3) & 31) . + static::$method((($b0 << 2) | ($b1 >> 6)) & 31) . + static::$method((($b1 >> 1) ) & 31) . + static::$method((($b1 << 4) | ($b2 >> 4)) & 31) . + static::$method((($b2 << 1) | ($b3 >> 7)) & 31) . + static::$method((($b3 >> 2) ) & 31) . + static::$method((($b3 << 3) ) & 31) . '='; } elseif ($i + 2 < $srcLen) { $b1 = $chunk[2]; $b2 = $chunk[3]; $dest .= - static::encode5BitsUpper( ($b0 >> 3) & 31) . - static::encode5BitsUpper((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5BitsUpper((($b1 >> 1) ) & 31) . - static::encode5BitsUpper((($b1 << 4) | ($b2 >> 4)) & 31) . - static::encode5BitsUpper((($b2 << 1) ) & 31) . + static::$method( ($b0 >> 3) & 31) . + static::$method((($b0 << 2) | ($b1 >> 6)) & 31) . + static::$method((($b1 >> 1) ) & 31) . + static::$method((($b1 << 4) | ($b2 >> 4)) & 31) . + static::$method((($b2 << 1) ) & 31) . '==='; } elseif ($i + 1 < $srcLen) { $b1 = $chunk[2]; $dest .= - static::encode5BitsUpper( ($b0 >> 3) & 31) . - static::encode5BitsUpper((($b0 << 2) | ($b1 >> 6)) & 31) . - static::encode5BitsUpper((($b1 >> 1) ) & 31) . - static::encode5BitsUpper((($b1 << 4) ) & 31) . + static::$method( ($b0 >> 3) & 31) . + static::$method((($b0 << 2) | ($b1 >> 6)) & 31) . + static::$method((($b1 >> 1) ) & 31) . + static::$method((($b1 << 4) ) & 31) . '===='; } else { $dest .= - static::encode5BitsUpper( ($b0 >> 3) & 31) . - static::encode5BitsUpper( ($b0 << 2) & 31) . + static::$method( ($b0 >> 3) & 31) . + static::$method( ($b0 << 2) & 31) . '======'; } } return $dest; } - - /** - * - * @param int $src - * @return int - */ - protected static function decode5Bits($src) - { - $ret = -1; - - // if ($src > 96 && $src < 123) $ret += $src - 97 + 1; // -64 - $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 96); - - // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 - $ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23); - - return $ret; - } - - /** - * - * @param int $src - * @return int - */ - protected static function decode5BitsUpper($src) - { - $ret = -1; - - // if ($src > 64 && $src < 91) $ret += $src - 65 + 1; // -64 - $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); - - // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 - $ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23); - - return $ret; - } - - /** - * @param $src - * @return string - */ - protected static function encode5Bits($src) - { - $diff = 0x61; - - // if ($src > 25) $ret -= 72; - $diff -= ((25 - $src) >> 8) & 73; - - return \pack('C', $src + $diff); - } - - /** - * @param $src - * @return string - */ - protected static function encode5BitsUpper($src) - { - $diff = 0x41; - - // if ($src > 25) $ret -= 40; - $diff -= ((25 - $src) >> 8) & 41; - - return \pack('C', $src + $diff); - } }
\ No newline at end of file diff --git a/src/Encoding.php b/src/Encoding.php index a8fadb0..d8d0004 100644 --- a/src/Encoding.php +++ b/src/Encoding.php @@ -36,6 +36,17 @@ abstract class Encoding } /** + * RFC 4648 Base32 encoding + * + * @param $str + * @return string + */ + public static function base32EncodeUpper($str) + { + return Base32::encodeUpper($str); + } + + /** * RFC 4648 Base32 decoding * * @param $str @@ -45,6 +56,18 @@ abstract class Encoding { return Base32::decode($str); } + + /** + * RFC 4648 Base32 decoding + * + * @param $str + * @return string + */ + public static function base32DecodeUpper($str) + { + return Base32::decodeUpper($str); + } + /** * RFC 4648 Base32 encoding * @@ -56,26 +79,27 @@ abstract class Encoding return Base32Hex::encode($str); } + /** - * RFC 4648 Base32 decoding + * RFC 4648 Base32 encoding * * @param $str * @return string */ - public static function base32HexDecode($str) + public static function base32HexEncodeUpper($str) { - return Base32Hex::decode($str); + return Base32Hex::encodeUpper($str); } /** - * RFC 4648 Base64 encoding + * RFC 4648 Base32 decoding * * @param $str * @return string */ - public static function base64Encode($str) + public static function base32HexDecode($str) { - return Base64::encode($str); + return Base32Hex::decode($str); } /** @@ -84,34 +108,31 @@ abstract class Encoding * @param $str * @return string */ - public static function base64Decode($str) + public static function base32HexDecodeUpper($str) { - return Base64::decode($str); + return Base32Hex::decodeUpper($str); } /** - * Convert a binary string into a hexadecimal string without cache-timing - * leaks + * RFC 4648 Base64 encoding * - * @param string $bin_string (raw binary) + * @param $str * @return string */ - public static function hexEncode($bin_string) + public static function base64Encode($str) { - return Hex::encode($bin_string); + return Base64::encode($str); } /** - * Convert a hexadecimal string into a binary string without cache-timing - * leaks + * RFC 4648 Base32 decoding * - * @param string $hex_string - * @return string (raw binary) - * @throws \RangeException + * @param $str + * @return string */ - public static function hexDecode($hex_string) + public static function base64Decode($str) { - return Hex::decode($hex_string); + return Base64::decode($str); } /** @@ -165,4 +186,53 @@ abstract class Encoding { return Base64DotSlashOrdered::decode($src); } + + /** + * Convert a binary string into a hexadecimal string without cache-timing + * leaks + * + * @param string $bin_string (raw binary) + * @return string + */ + public static function hexEncode($bin_string) + { + return Hex::encode($bin_string); + } + + /** + * Convert a hexadecimal string into a binary string without cache-timing + * leaks + * + * @param string $hex_string + * @return string (raw binary) + * @throws \RangeException + */ + public static function hexDecode($hex_string) + { + return Hex::decode($hex_string); + } + + /** + * Convert a binary string into a hexadecimal string without cache-timing + * leaks + * + * @param string $bin_string (raw binary) + * @return string + */ + public static function hexEncodeUpper($bin_string) + { + return Hex::encodeUpper($bin_string); + } + + /** + * Convert a binary string into a hexadecimal string without cache-timing + * leaks + * + * @param string $bin_string (raw binary) + * @return string + */ + public static function hexDecodeUpper($bin_string) + { + return Hex::decode($bin_string); + } } |