Skip to content

Commit

Permalink
Improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 26, 2024
1 parent 0c37300 commit 2914d90
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,18 @@ public function decode(string $encoded, bool $strict = false): string
throw new RuntimeException('The encoded data length is invalid.');
}

$encoded .= str_repeat($padding, $remainder);
for ($i = 0; $i < $remainder; $i++) {
$encoded .= $padding;
}
}

$inside = rtrim($encoded, $padding);
$end = substr($encoded, strlen($inside));
$endLength = strlen($end);
if ($strict && 0 !== $endLength && 1 !== $endLength && 3 !== $endLength && 4 !== $endLength && 6 !== $endLength) {
throw new RuntimeException('The encoded data ends with an invalid padding sequence length.');
if ($strict) {
$endLength = strlen($end);
if (0 !== $endLength && 1 !== $endLength && 3 !== $endLength && 4 !== $endLength && 6 !== $endLength) {
throw new RuntimeException('The encoded data ends with an invalid padding sequence length.');
}
}

if (false !== strpos($inside, $padding)) {
Expand All @@ -127,10 +131,11 @@ public function decode(string $encoded, bool $strict = false): string
}

$chars = [];
foreach (str_split($alphabet) as $offset => $char) {
$chars[$char] = $offset;
for ($i = 0; $i < self::ALPHABET_SIZE; $i++) {
$chars[$alphabet[$i]] = $i;
}
$chars[$padding] = 0;

$offset = 0;
$bitLen = 5;
$length = strlen($encoded);
Expand Down

0 comments on commit 2914d90

Please sign in to comment.