summaryrefslogtreecommitdiff
path: root/vendor/paragonie/constant_time_encoding/src/Base32.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/paragonie/constant_time_encoding/src/Base32.php')
-rw-r--r--vendor/paragonie/constant_time_encoding/src/Base32.php78
1 files changed, 63 insertions, 15 deletions
diff --git a/vendor/paragonie/constant_time_encoding/src/Base32.php b/vendor/paragonie/constant_time_encoding/src/Base32.php
index 7784bafbf..7508b3df6 100644
--- a/vendor/paragonie/constant_time_encoding/src/Base32.php
+++ b/vendor/paragonie/constant_time_encoding/src/Base32.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
@@ -60,20 +64,20 @@ abstract class Base32 implements EncoderInterface
/**
* Encode into Base32 (RFC 4648)
*
- * @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, false, true);
+ return static::doEncode($binString, false, true);
}
/**
* Encode into Base32 (RFC 4648)
*
* @param string $src
* @return string
- * @throws \TypeError
+ * @throws TypeError
*/
public static function encodeUnpadded(string $src): string
{
@@ -85,7 +89,7 @@ abstract class Base32 implements EncoderInterface
*
* @param string $src
* @return string
- * @throws \TypeError
+ * @throws TypeError
*/
public static function encodeUpper(string $src): string
{
@@ -97,7 +101,7 @@ abstract class Base32 implements EncoderInterface
*
* @param string $src
* @return string
- * @throws \TypeError
+ * @throws TypeError
*/
public static function encodeUpperUnpadded(string $src): string
{
@@ -182,6 +186,32 @@ abstract class Base32 implements EncoderInterface
return \pack('C', $src + $diff);
}
+ /**
+ * @param string $encodedString
+ * @param bool $upper
+ * @return string
+ */
+ public static function decodeNoPadding(string $encodedString, bool $upper = false): string
+ {
+ $srcLen = Binary::safeStrlen($encodedString);
+ if ($srcLen === 0) {
+ return '';
+ }
+ if (($srcLen & 7) === 0) {
+ for ($j = 0; $j < 7 && $j < $srcLen; ++$j) {
+ if ($encodedString[$srcLen - $j - 1] === '=') {
+ throw new InvalidArgumentException(
+ "decodeNoPadding() doesn't tolerate padding"
+ );
+ }
+ }
+ }
+ return static::doDecode(
+ $encodedString,
+ $upper,
+ true
+ );
+ }
/**
* Base32 decoding
@@ -190,11 +220,15 @@ abstract class Base32 implements EncoderInterface
* @param bool $upper
* @param bool $strictPadding
* @return string
- * @throws \TypeError
+ *
+ * @throws TypeError
* @psalm-suppress RedundantCondition
*/
- protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
- {
+ protected static function doDecode(
+ string $src,
+ bool $upper = false,
+ bool $strictPadding = false
+ ): string {
// We do this to reduce code duplication:
$method = $upper
? 'decode5BitsUpper'
@@ -216,7 +250,7 @@ abstract class Base32 implements EncoderInterface
}
}
if (($srcLen & 7) === 1) {
- throw new \RangeException(
+ throw new RangeException(
'Incorrect padding'
);
}
@@ -287,6 +321,9 @@ abstract class Base32 implements EncoderInterface
(($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff
);
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8;
+ if ($strictPadding) {
+ $err |= ($c6 << 5) & 0xff;
+ }
} elseif ($i + 5 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
@@ -324,6 +361,9 @@ abstract class Base32 implements EncoderInterface
(($c3 << 4) | ($c4 >> 1) ) & 0xff
);
$err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8;
+ if ($strictPadding) {
+ $err |= ($c4 << 7) & 0xff;
+ }
} elseif ($i + 3 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
@@ -338,6 +378,9 @@ abstract class Base32 implements EncoderInterface
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff
);
$err |= ($c0 | $c1 | $c2 | $c3) >> 8;
+ if ($strictPadding) {
+ $err |= ($c3 << 4) & 0xff;
+ }
} elseif ($i + 2 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
@@ -350,6 +393,9 @@ abstract class Base32 implements EncoderInterface
(($c1 << 6) | ($c2 << 1) ) & 0xff
);
$err |= ($c0 | $c1 | $c2) >> 8;
+ if ($strictPadding) {
+ $err |= ($c2 << 6) & 0xff;
+ }
} elseif ($i + 1 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
@@ -359,6 +405,9 @@ abstract class Base32 implements EncoderInterface
(($c0 << 3) | ($c1 >> 2) ) & 0xff
);
$err |= ($c0 | $c1) >> 8;
+ if ($strictPadding) {
+ $err |= ($c1 << 6) & 0xff;
+ }
} else {
$dest .= \pack(
'C',
@@ -367,10 +416,9 @@ abstract class Base32 implements EncoderInterface
$err |= ($c0) >> 8;
}
}
- /** @var bool $check */
$check = ($err === 0);
if (!$check) {
- throw new \RangeException(
+ throw new RangeException(
'Base32::doDecode() only expects characters in the correct base32 alphabet'
);
}
@@ -384,7 +432,7 @@ abstract class Base32 implements EncoderInterface
* @param bool $upper
* @param bool $pad
* @return string
- * @throws \TypeError
+ * @throws TypeError
*/
protected static function doEncode(string $src, bool $upper = false, $pad = true): string
{