summaryrefslogtreecommitdiff
path: root/vendor/paragonie/constant_time_encoding/src/Base64.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/paragonie/constant_time_encoding/src/Base64.php')
-rw-r--r--vendor/paragonie/constant_time_encoding/src/Base64.php71
1 files changed, 57 insertions, 14 deletions
diff --git a/vendor/paragonie/constant_time_encoding/src/Base64.php b/vendor/paragonie/constant_time_encoding/src/Base64.php
index 4739e4895..f5716179f 100644
--- a/vendor/paragonie/constant_time_encoding/src/Base64.php
+++ b/vendor/paragonie/constant_time_encoding/src/Base64.php
@@ -2,8 +2,12 @@
declare(strict_types=1);
namespace ParagonIE\ConstantTime;
+use InvalidArgumentException;
+use RangeException;
+use TypeError;
+
/**
- * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
+ * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -38,13 +42,14 @@ abstract class Base64 implements EncoderInterface
*
* Base64 character set "[A-Z][a-z][0-9]+/"
*
- * @param string $src
+ * @param string $binString
* @return string
- * @throws \TypeError
+ *
+ * @throws TypeError
*/
- public static function encode(string $src): string
+ public static function encode(string $binString): string
{
- return static::doEncode($src, true);
+ return static::doEncode($binString, true);
}
/**
@@ -54,7 +59,8 @@ abstract class Base64 implements EncoderInterface
*
* @param string $src
* @return string
- * @throws \TypeError
+ *
+ * @throws TypeError
*/
public static function encodeUnpadded(string $src): string
{
@@ -65,7 +71,8 @@ abstract class Base64 implements EncoderInterface
* @param string $src
* @param bool $pad Include = padding?
* @return string
- * @throws \TypeError
+ *
+ * @throws TypeError
*/
protected static function doEncode(string $src, bool $pad = true): string
{
@@ -119,8 +126,9 @@ abstract class Base64 implements EncoderInterface
* @param string $encodedString
* @param bool $strictPadding
* @return string
- * @throws \RangeException
- * @throws \TypeError
+ *
+ * @throws RangeException
+ * @throws TypeError
* @psalm-suppress RedundantCondition
*/
public static function decode(string $encodedString, bool $strictPadding = false): string
@@ -141,12 +149,12 @@ abstract class Base64 implements EncoderInterface
}
}
if (($srcLen & 3) === 1) {
- throw new \RangeException(
+ throw new RangeException(
'Incorrect padding'
);
}
if ($encodedString[$srcLen - 1] === '=') {
- throw new \RangeException(
+ throw new RangeException(
'Incorrect padding'
);
}
@@ -189,6 +197,9 @@ abstract class Base64 implements EncoderInterface
((($c1 << 4) | ($c2 >> 2)) & 0xff)
);
$err |= ($c0 | $c1 | $c2) >> 8;
+ if ($strictPadding) {
+ $err |= ($c2 << 6) & 0xff;
+ }
} elseif ($i + 1 < $srcLen) {
$c1 = static::decode6Bits($chunk[2]);
$dest .= \pack(
@@ -196,14 +207,16 @@ abstract class Base64 implements EncoderInterface
((($c0 << 2) | ($c1 >> 4)) & 0xff)
);
$err |= ($c0 | $c1) >> 8;
- } elseif ($i < $srcLen && $strictPadding) {
+ if ($strictPadding) {
+ $err |= ($c1 << 4) & 0xff;
+ }
+ } elseif ($strictPadding) {
$err |= 1;
}
}
- /** @var bool $check */
$check = ($err === 0);
if (!$check) {
- throw new \RangeException(
+ throw new RangeException(
'Base64::decode() only expects characters in the correct base64 alphabet'
);
}
@@ -211,6 +224,36 @@ abstract class Base64 implements EncoderInterface
}
/**
+ * @param string $encodedString
+ * @return string
+ */
+ public static function decodeNoPadding(string $encodedString): string
+ {
+ $srcLen = Binary::safeStrlen($encodedString);
+ if ($srcLen === 0) {
+ return '';
+ }
+ if (($srcLen & 3) === 0) {
+ if ($encodedString[$srcLen - 1] === '=') {
+ throw new InvalidArgumentException(
+ "decodeNoPadding() doesn't tolerate padding"
+ );
+ }
+ if (($srcLen & 3) > 1) {
+ if ($encodedString[$srcLen - 2] === '=') {
+ throw new InvalidArgumentException(
+ "decodeNoPadding() doesn't tolerate padding"
+ );
+ }
+ }
+ }
+ return static::decode(
+ $encodedString,
+ true
+ );
+ }
+
+ /**
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
* into 8-bit integers.
*