diff --git a/src/Base32.php b/src/Base32.php index 45d5333..b8dee0a 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -221,7 +221,7 @@ public static function decodeNoPadding( string $encodedString, bool $upper = false ): string { - $srcLen = Binary::safeStrlen($encodedString); + $srcLen = \strlen($encodedString); if ($srcLen === 0) { return ''; } @@ -263,7 +263,7 @@ protected static function doDecode( : 'decode5Bits'; // Remove padding - $srcLen = Binary::safeStrlen($src); + $srcLen = \strlen($src); if ($srcLen === 0) { return ''; } @@ -284,7 +284,7 @@ protected static function doDecode( } } else { $src = \rtrim($src, '='); - $srcLen = Binary::safeStrlen($src); + $srcLen = \strlen($src); } $err = 0; @@ -292,7 +292,7 @@ protected static function doDecode( // Main loop (no padding): for ($i = 0; $i + 8 <= $srcLen; $i += 8) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8)); + $chunk = \unpack('C*', \substr($src, $i, 8)); /** @var int $c0 */ $c0 = static::$method($chunk[1]); /** @var int $c1 */ @@ -323,7 +323,7 @@ protected static function doDecode( // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); + $chunk = \unpack('C*', \substr($src, $i, $srcLen - $i)); /** @var int $c0 */ $c0 = static::$method($chunk[1]); @@ -474,12 +474,12 @@ protected static function doEncode( : 'encode5Bits'; $dest = ''; - $srcLen = Binary::safeStrlen($src); + $srcLen = \strlen($src); // Main loop (no padding): for ($i = 0; $i + 5 <= $srcLen; $i += 5) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 5)); + $chunk = \unpack('C*', \substr($src, $i, 5)); $b0 = $chunk[1]; $b1 = $chunk[2]; $b2 = $chunk[3]; @@ -498,7 +498,7 @@ protected static function doEncode( // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); + $chunk = \unpack('C*', \substr($src, $i, $srcLen - $i)); $b0 = $chunk[1]; if ($i + 3 < $srcLen) { $b1 = $chunk[2]; diff --git a/src/Base64.php b/src/Base64.php index bd18203..cac557f 100644 --- a/src/Base64.php +++ b/src/Base64.php @@ -115,11 +115,11 @@ protected static function doEncode( bool $pad = true ): string { $dest = ''; - $srcLen = Binary::safeStrlen($src); + $srcLen = \strlen($src); // Main loop (no padding): for ($i = 0; $i + 3 <= $srcLen; $i += 3) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3)); + $chunk = \unpack('C*', \substr($src, $i, 3)); $b0 = $chunk[1]; $b1 = $chunk[2]; $b2 = $chunk[3]; @@ -133,7 +133,7 @@ protected static function doEncode( // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); + $chunk = \unpack('C*', \substr($src, $i, $srcLen - $i)); $b0 = $chunk[1]; if ($i + 1 < $srcLen) { $b1 = $chunk[2]; @@ -175,7 +175,7 @@ public static function decode( bool $strictPadding = false ): string { // Remove padding - $srcLen = Binary::safeStrlen($encodedString); + $srcLen = \strlen($encodedString); if ($srcLen === 0) { return ''; } @@ -215,7 +215,7 @@ public static function decode( } } else { $encodedString = \rtrim($encodedString, '='); - $srcLen = Binary::safeStrlen($encodedString); + $srcLen = \strlen($encodedString); } $err = 0; @@ -223,7 +223,7 @@ public static function decode( // Main loop (no padding): for ($i = 0; $i + 4 <= $srcLen; $i += 4) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, 4)); + $chunk = \unpack('C*', \substr($encodedString, $i, 4)); $c0 = static::decode6Bits($chunk[1]); $c1 = static::decode6Bits($chunk[2]); $c2 = static::decode6Bits($chunk[3]); @@ -240,7 +240,7 @@ public static function decode( // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array $chunk */ - $chunk = \unpack('C*', Binary::safeSubstr($encodedString, $i, $srcLen - $i)); + $chunk = \unpack('C*', \substr($encodedString, $i, $srcLen - $i)); $c0 = static::decode6Bits($chunk[1]); if ($i + 2 < $srcLen) { @@ -287,7 +287,7 @@ public static function decodeNoPadding( #[\SensitiveParameter] string $encodedString ): string { - $srcLen = Binary::safeStrlen($encodedString); + $srcLen = \strlen($encodedString); if ($srcLen === 0) { return ''; } diff --git a/src/Binary.php b/src/Binary.php index a958f2f..7e52f7c 100644 --- a/src/Binary.php +++ b/src/Binary.php @@ -49,13 +49,7 @@ public static function safeStrlen( #[\SensitiveParameter] string $str ): int { - if (\function_exists('mb_strlen')) { - // mb_strlen in PHP 7.x can return false. - /** @psalm-suppress RedundantCast */ - return (int) \mb_strlen($str, '8bit'); - } else { - return \strlen($str); - } + return \strlen($str); } /** @@ -80,9 +74,6 @@ public static function safeSubstr( if ($length === 0) { return ''; } - if (\function_exists('mb_substr')) { - return \mb_substr($str, $start, $length, '8bit'); - } // Unlike mb_substr(), substr() doesn't accept NULL for length if ($length !== null) { return \substr($str, $start, $length); diff --git a/src/Hex.php b/src/Hex.php index 72c7918..d4d5259 100644 --- a/src/Hex.php +++ b/src/Hex.php @@ -56,7 +56,7 @@ public static function encode( } } $hex = ''; - $len = Binary::safeStrlen($binString); + $len = \strlen($binString); for ($i = 0; $i < $len; ++$i) { /** @var array $chunk */ $chunk = \unpack('C', $binString[$i]); @@ -85,7 +85,7 @@ public static function encodeUpper( string $binString ): string { $hex = ''; - $len = Binary::safeStrlen($binString); + $len = \strlen($binString); for ($i = 0; $i < $len; ++$i) { /** @var array $chunk */ @@ -127,7 +127,7 @@ public static function decode( $hex_pos = 0; $bin = ''; $c_acc = 0; - $hex_len = Binary::safeStrlen($encodedString); + $hex_len = \strlen($encodedString); $state = 0; if (($hex_len & 1) !== 0) { if ($strictPadding) { diff --git a/tests/Base64UrlSafeTest.php b/tests/Base64UrlSafeTest.php index 73e8fbf..2d6fe19 100644 --- a/tests/Base64UrlSafeTest.php +++ b/tests/Base64UrlSafeTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ParagonIE\ConstantTime\Base64UrlSafe; -use ParagonIE\ConstantTime\Binary; use RangeException; use TypeError; @@ -49,7 +48,7 @@ public function testRandom() $random = \random_bytes(1 << 20); $enc = Base64UrlSafe::encode($random); - $this->assertTrue(Binary::safeStrlen($enc) > 65536); + $this->assertTrue(\strlen($enc) > 65536); $this->assertSame( $random, Base64UrlSafe::decode($enc) diff --git a/tests/CanonicalTrait.php b/tests/CanonicalTrait.php index bd85617..c3b3ba4 100644 --- a/tests/CanonicalTrait.php +++ b/tests/CanonicalTrait.php @@ -2,8 +2,6 @@ declare(strict_types=1); namespace ParagonIE\ConstantTime\Tests; -use ParagonIE\ConstantTime\Binary; - trait CanonicalTrait { public static function canonicalDataProvider(): array @@ -22,8 +20,8 @@ abstract protected function getNextChar(string $c): string; protected function increment(string $str): string { - $i = Binary::safeStrlen($str) - 1; + $i = \strlen($str) - 1; $c = $this->getNextChar($str[$i]); - return Binary::safeSubstr($str, 0, $i) . $c; + return \substr($str, 0, $i) . $c; } }