Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -263,7 +263,7 @@ protected static function doDecode(
: 'decode5Bits';

// Remove padding
$srcLen = Binary::safeStrlen($src);
$srcLen = \strlen($src);
if ($srcLen === 0) {
return '';
}
Expand All @@ -284,15 +284,15 @@ protected static function doDecode(
}
} else {
$src = \rtrim($src, '=');
$srcLen = Binary::safeStrlen($src);
$srcLen = \strlen($src);
}

$err = 0;
$dest = '';
// Main loop (no padding):
for ($i = 0; $i + 8 <= $srcLen; $i += 8) {
/** @var array<int, int> $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 */
Expand Down Expand Up @@ -323,7 +323,7 @@ protected static function doDecode(
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $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]);

Expand Down Expand Up @@ -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<int, int> $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];
Expand All @@ -498,7 +498,7 @@ protected static function doEncode(
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $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];
Expand Down
16 changes: 8 additions & 8 deletions src/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int> $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];
Expand All @@ -133,7 +133,7 @@ protected static function doEncode(
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $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];
Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -215,15 +215,15 @@ public static function decode(
}
} else {
$encodedString = \rtrim($encodedString, '=');
$srcLen = Binary::safeStrlen($encodedString);
$srcLen = \strlen($encodedString);
}

$err = 0;
$dest = '';
// Main loop (no padding):
for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
/** @var array<int, int> $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]);
Expand All @@ -240,7 +240,7 @@ public static function decode(
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $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) {
Expand Down Expand Up @@ -287,7 +287,7 @@ public static function decodeNoPadding(
#[\SensitiveParameter]
string $encodedString
): string {
$srcLen = Binary::safeStrlen($encodedString);
$srcLen = \strlen($encodedString);
if ($srcLen === 0) {
return '';
}
Expand Down
11 changes: 1 addition & 10 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function encode(
}
}
$hex = '';
$len = Binary::safeStrlen($binString);
$len = \strlen($binString);
for ($i = 0; $i < $len; ++$i) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C', $binString[$i]);
Expand Down Expand Up @@ -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<int, int> $chunk */
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions tests/Base64UrlSafeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions tests/CanonicalTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
declare(strict_types=1);
namespace ParagonIE\ConstantTime\Tests;

use ParagonIE\ConstantTime\Binary;

trait CanonicalTrait
{
public static function canonicalDataProvider(): array
Expand All @@ -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;
}
}