Skip to content

Commit 33f30ed

Browse files
Merge pull request #67 from peldax/patch-1
FIX: Trim padding characters from encoded string before calling sodium decode
2 parents 5bd79e0 + 693c810 commit 33f30ed

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/Base64.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,24 @@ public static function decode(
213213
'Incorrect padding'
214214
);
215215
}
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($encodedString, $variant);
225-
} catch (SodiumException $ex) {
226-
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
227-
}
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);
228232
}
229233
}
230-
} else {
231-
$encodedString = rtrim($encodedString, '=');
232-
$srcLen = strlen($encodedString);
233234
}
234235

235236
$err = 0;

0 commit comments

Comments
 (0)