Skip to content

Commit 2947162

Browse files
Revert #67 and fix padding stripping
1 parent 9140114 commit 2947162

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

src/Base64.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,24 @@ public static function decode(
213213
'Incorrect padding'
214214
);
215215
}
216-
}
217-
218-
$encodedString = rtrim($encodedString, '=');
219-
$srcLen = strlen($encodedString);
220-
221-
if (extension_loaded('sodium')) {
222-
$variant = match(static::class) {
223-
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
224-
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
225-
default => 0,
226-
};
227-
if ($variant > 0) {
228-
try {
229-
return sodium_base642bin($encodedString, $variant);
230-
} catch (SodiumException $ex) {
231-
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
216+
if (extension_loaded('sodium')) {
217+
$variant = match(static::class) {
218+
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING,
219+
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING,
220+
default => 0,
221+
};
222+
if ($variant > 0) {
223+
try {
224+
return sodium_base642bin(substr($encodedString, 0, $srcLen), $variant);
225+
} catch (SodiumException $ex) {
226+
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
227+
}
232228
}
233229
}
230+
} else {
231+
// Just remove all padding.
232+
$encodedString = rtrim($encodedString, '=');
233+
$srcLen = strlen($encodedString);
234234
}
235235

236236
$err = 0;

0 commit comments

Comments
 (0)