upgrade idiorm to php8.1-patched version (aaronpk/idiorm)
This commit is contained in:
+63
-15
@@ -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
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
+57
-14
@@ -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,20 +207,52 @@ 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'
|
||||
);
|
||||
}
|
||||
return $dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
+8
-3
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
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
|
||||
@@ -46,6 +48,8 @@ abstract class Binary
|
||||
public static function safeStrlen(string $str): int
|
||||
{
|
||||
if (\function_exists('mb_strlen')) {
|
||||
// mb_strlen in PHP 7.x can return false.
|
||||
/** @psalm-suppress RedundantCast */
|
||||
return (int) \mb_strlen($str, '8bit');
|
||||
} else {
|
||||
return \strlen($str);
|
||||
@@ -60,9 +64,10 @@ abstract class Binary
|
||||
* @staticvar boolean $exists
|
||||
* @param string $str
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
* @param ?int $length
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function safeSubstr(
|
||||
string $str,
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
+19
-17
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
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
|
||||
@@ -36,7 +38,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Encode(string $str): string
|
||||
{
|
||||
@@ -48,7 +50,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32EncodeUpper(string $str): string
|
||||
{
|
||||
@@ -60,7 +62,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Decode(string $str): string
|
||||
{
|
||||
@@ -72,7 +74,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32DecodeUpper(string $str): string
|
||||
{
|
||||
@@ -84,7 +86,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncode(string $str): string
|
||||
{
|
||||
@@ -96,7 +98,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncodeUpper(string $str): string
|
||||
{
|
||||
@@ -108,7 +110,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecode(string $str): string
|
||||
{
|
||||
@@ -120,7 +122,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecodeUpper(string $str): string
|
||||
{
|
||||
@@ -132,7 +134,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Encode(string $str): string
|
||||
{
|
||||
@@ -144,7 +146,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Decode(string $str): string
|
||||
{
|
||||
@@ -157,7 +159,7 @@ abstract class Encoding
|
||||
* Base64 character set "./[A-Z][a-z][0-9]"
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64EncodeDotSlash(string $str): string
|
||||
{
|
||||
@@ -172,7 +174,7 @@ abstract class Encoding
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \RangeException
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64DecodeDotSlash(string $str): string
|
||||
{
|
||||
@@ -185,7 +187,7 @@ abstract class Encoding
|
||||
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64EncodeDotSlashOrdered(string $str): string
|
||||
{
|
||||
@@ -200,7 +202,7 @@ abstract class Encoding
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \RangeException
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64DecodeDotSlashOrdered(string $str): string
|
||||
{
|
||||
@@ -213,7 +215,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $bin_string (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hexEncode(string $bin_string): string
|
||||
{
|
||||
@@ -239,7 +241,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $bin_string (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hexEncodeUpper(string $bin_string): string
|
||||
{
|
||||
|
||||
+17
-30
@@ -2,8 +2,11 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
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
|
||||
@@ -37,22 +40,19 @@ abstract class Hex implements EncoderInterface
|
||||
*
|
||||
* @param string $binString (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encode(string $binString): string
|
||||
{
|
||||
/** @var string $hex */
|
||||
$hex = '';
|
||||
$len = Binary::safeStrlen($binString);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = \unpack('C', Binary::safeSubstr($binString, $i, 1));
|
||||
/** @var int $c */
|
||||
$chunk = \unpack('C', $binString[$i]);
|
||||
$c = $chunk[1] & 0xf;
|
||||
/** @var int $b */
|
||||
$b = $chunk[1] >> 4;
|
||||
|
||||
$hex .= pack(
|
||||
$hex .= \pack(
|
||||
'CC',
|
||||
(87 + $b + ((($b - 10) >> 8) & ~38)),
|
||||
(87 + $c + ((($c - 10) >> 8) & ~38))
|
||||
@@ -67,24 +67,20 @@ abstract class Hex implements EncoderInterface
|
||||
*
|
||||
* @param string $binString (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUpper(string $binString): string
|
||||
{
|
||||
/** @var string $hex */
|
||||
$hex = '';
|
||||
/** @var int $len */
|
||||
$len = Binary::safeStrlen($binString);
|
||||
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = \unpack('C', Binary::safeSubstr($binString, $i, 2));
|
||||
/** @var int $c */
|
||||
$chunk = \unpack('C', $binString[$i]);
|
||||
$c = $chunk[1] & 0xf;
|
||||
/** @var int $b */
|
||||
$b = $chunk[1] >> 4;
|
||||
|
||||
$hex .= pack(
|
||||
$hex .= \pack(
|
||||
'CC',
|
||||
(55 + $b + ((($b - 10) >> 8) & ~6)),
|
||||
(55 + $c + ((($c - 10) >> 8) & ~6))
|
||||
@@ -100,23 +96,20 @@ abstract class Hex implements EncoderInterface
|
||||
* @param string $encodedString
|
||||
* @param bool $strictPadding
|
||||
* @return string (raw binary)
|
||||
* @throws \RangeException
|
||||
* @throws RangeException
|
||||
*/
|
||||
public static function decode(string $encodedString, bool $strictPadding = false): string
|
||||
{
|
||||
/** @var int $hex_pos */
|
||||
public static function decode(
|
||||
string $encodedString,
|
||||
bool $strictPadding = false
|
||||
): string {
|
||||
$hex_pos = 0;
|
||||
/** @var string $bin */
|
||||
$bin = '';
|
||||
/** @var int $c_acc */
|
||||
$c_acc = 0;
|
||||
/** @var int $hex_len */
|
||||
$hex_len = Binary::safeStrlen($encodedString);
|
||||
/** @var int $state */
|
||||
$state = 0;
|
||||
if (($hex_len & 1) !== 0) {
|
||||
if ($strictPadding) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Expected an even number of hexadecimal characters'
|
||||
);
|
||||
} else {
|
||||
@@ -129,23 +122,17 @@ abstract class Hex implements EncoderInterface
|
||||
$chunk = \unpack('C*', $encodedString);
|
||||
while ($hex_pos < $hex_len) {
|
||||
++$hex_pos;
|
||||
/** @var int $c */
|
||||
$c = $chunk[$hex_pos];
|
||||
/** @var int $c_num */
|
||||
$c_num = $c ^ 48;
|
||||
/** @var int $c_num0 */
|
||||
$c_num0 = ($c_num - 10) >> 8;
|
||||
/** @var int $c_alpha */
|
||||
$c_alpha = ($c & ~32) - 55;
|
||||
/** @var int $c_alpha0 */
|
||||
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
|
||||
|
||||
if (($c_num0 | $c_alpha0) === 0) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Expected hexadecimal character'
|
||||
);
|
||||
}
|
||||
/** @var int $c_val */
|
||||
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
|
||||
if ($state === 0) {
|
||||
$c_acc = $c_val * 16;
|
||||
|
||||
+21
-10
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
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
|
||||
@@ -41,7 +43,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Encode(string $str): string
|
||||
{
|
||||
@@ -55,7 +58,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Decode(string $str): string
|
||||
{
|
||||
@@ -69,7 +73,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64UrlSafeEncode(string $str): string
|
||||
{
|
||||
@@ -83,7 +88,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64UrlSafeDecode(string $str): string
|
||||
{
|
||||
@@ -97,7 +103,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Encode(string $str): string
|
||||
{
|
||||
@@ -111,7 +118,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Decode(string $str): string
|
||||
{
|
||||
@@ -125,7 +133,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncode(string $str): string
|
||||
{
|
||||
@@ -139,7 +148,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecode(string $str): string
|
||||
{
|
||||
@@ -153,7 +163,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base16Encode(string $str): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user