From 58c3f47f650c94ec05a151692652a868995d2938 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Tue, 14 Jun 2022 02:56:20 -0400 Subject: [PATCH] Boyscouting --- src/Base32.php | 24 +++++++++++++++--------- src/Base64.php | 15 ++++++++++----- src/Binary.php | 5 ++++- src/Encoding.php | 34 ++++++++++++++++++---------------- src/Hex.php | 19 ++++++++++++------- src/RFC4648.php | 29 ++++++++++++++++++++--------- 6 files changed, 79 insertions(+), 47 deletions(-) diff --git a/src/Base32.php b/src/Base32.php index 5054d73..7508b3d 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use RangeException; +use TypeError; /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. @@ -65,7 +66,7 @@ public static function decodeUpper(string $src, bool $strictPadding = false): st * * @param string $binString * @return string - * @throws \TypeError + * @throws TypeError */ public static function encode(string $binString): string { @@ -76,7 +77,7 @@ public static function encode(string $binString): string * * @param string $src * @return string - * @throws \TypeError + * @throws TypeError */ public static function encodeUnpadded(string $src): string { @@ -88,7 +89,7 @@ public static function encodeUnpadded(string $src): string * * @param string $src * @return string - * @throws \TypeError + * @throws TypeError */ public static function encodeUpper(string $src): string { @@ -100,7 +101,7 @@ public static function encodeUpper(string $src): string * * @param string $src * @return string - * @throws \TypeError + * @throws TypeError */ public static function encodeUpperUnpadded(string $src): string { @@ -187,6 +188,7 @@ protected static function encode5BitsUpper(int $src): string /** * @param string $encodedString + * @param bool $upper * @return string */ public static function decodeNoPadding(string $encodedString, bool $upper = false): string @@ -218,11 +220,15 @@ public static function decodeNoPadding(string $encodedString, bool $upper = fals * @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' @@ -244,7 +250,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric } } if (($srcLen & 7) === 1) { - throw new \RangeException( + throw new RangeException( 'Incorrect padding' ); } @@ -426,7 +432,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric * @param bool $upper * @param bool $pad * @return string - * @throws \TypeError + * @throws TypeError */ protected static function doEncode(string $src, bool $upper = false, $pad = true): string { diff --git a/src/Base64.php b/src/Base64.php index 5422aa4..f571617 100644 --- a/src/Base64.php +++ b/src/Base64.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use RangeException; +use TypeError; /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. @@ -43,7 +44,8 @@ abstract class Base64 implements EncoderInterface * * @param string $binString * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function encode(string $binString): string { @@ -57,7 +59,8 @@ public static function encode(string $binString): string * * @param string $src * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function encodeUnpadded(string $src): string { @@ -68,7 +71,8 @@ public static function encodeUnpadded(string $src): string * @param string $src * @param bool $pad Include = padding? * @return string - * @throws \TypeError + * + * @throws TypeError */ protected static function doEncode(string $src, bool $pad = true): string { @@ -122,8 +126,9 @@ protected static function doEncode(string $src, bool $pad = true): string * @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 diff --git a/src/Binary.php b/src/Binary.php index 4a36572..828f3e0 100644 --- a/src/Binary.php +++ b/src/Binary.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace ParagonIE\ConstantTime; +use TypeError; + /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) @@ -64,7 +66,8 @@ public static function safeStrlen(string $str): int * @param int $start * @param ?int $length * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function safeSubstr( string $str, diff --git a/src/Encoding.php b/src/Encoding.php index 1336935..8649f31 100644 --- a/src/Encoding.php +++ b/src/Encoding.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace ParagonIE\ConstantTime; +use TypeError; + /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) @@ -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 @@ public static function base32Encode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32EncodeUpper(string $str): string { @@ -60,7 +62,7 @@ public static function base32EncodeUpper(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32Decode(string $str): string { @@ -72,7 +74,7 @@ public static function base32Decode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32DecodeUpper(string $str): string { @@ -84,7 +86,7 @@ public static function base32DecodeUpper(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32HexEncode(string $str): string { @@ -96,7 +98,7 @@ public static function base32HexEncode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32HexEncodeUpper(string $str): string { @@ -108,7 +110,7 @@ public static function base32HexEncodeUpper(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32HexDecode(string $str): string { @@ -120,7 +122,7 @@ public static function base32HexDecode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base32HexDecodeUpper(string $str): string { @@ -132,7 +134,7 @@ public static function base32HexDecodeUpper(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base64Encode(string $str): string { @@ -144,7 +146,7 @@ public static function base64Encode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * @throws TypeError */ public static function base64Decode(string $str): string { @@ -157,7 +159,7 @@ public static function base64Decode(string $str): string * 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 @@ public static function base64EncodeDotSlash(string $str): string * @param string $str * @return string * @throws \RangeException - * @throws \TypeError + * @throws TypeError */ public static function base64DecodeDotSlash(string $str): string { @@ -185,7 +187,7 @@ public static function base64DecodeDotSlash(string $str): string * 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 @@ public static function base64EncodeDotSlashOrdered(string $str): string * @param string $str * @return string * @throws \RangeException - * @throws \TypeError + * @throws TypeError */ public static function base64DecodeDotSlashOrdered(string $str): string { @@ -213,7 +215,7 @@ public static function base64DecodeDotSlashOrdered(string $str): string * * @param string $bin_string (raw binary) * @return string - * @throws \TypeError + * @throws TypeError */ public static function hexEncode(string $bin_string): string { @@ -239,7 +241,7 @@ public static function hexDecode(string $hex_string): string * * @param string $bin_string (raw binary) * @return string - * @throws \TypeError + * @throws TypeError */ public static function hexEncodeUpper(string $bin_string): string { diff --git a/src/Hex.php b/src/Hex.php index bf3b63d..a9e058c 100644 --- a/src/Hex.php +++ b/src/Hex.php @@ -2,6 +2,9 @@ declare(strict_types=1); namespace ParagonIE\ConstantTime; +use RangeException; +use TypeError; + /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) @@ -37,7 +40,7 @@ abstract class Hex implements EncoderInterface * * @param string $binString (raw binary) * @return string - * @throws \TypeError + * @throws TypeError */ public static function encode(string $binString): string { @@ -64,7 +67,7 @@ public static function encode(string $binString): string * * @param string $binString (raw binary) * @return string - * @throws \TypeError + * @throws TypeError */ public static function encodeUpper(string $binString): string { @@ -93,10 +96,12 @@ public static function encodeUpper(string $binString): string * @param string $encodedString * @param bool $strictPadding * @return string (raw binary) - * @throws \RangeException + * @throws RangeException */ - public static function decode(string $encodedString, bool $strictPadding = false): string - { + public static function decode( + string $encodedString, + bool $strictPadding = false + ): string { $hex_pos = 0; $bin = ''; $c_acc = 0; @@ -104,7 +109,7 @@ public static function decode(string $encodedString, bool $strictPadding = false $state = 0; if (($hex_len & 1) !== 0) { if ($strictPadding) { - throw new \RangeException( + throw new RangeException( 'Expected an even number of hexadecimal characters' ); } else { @@ -124,7 +129,7 @@ public static function decode(string $encodedString, bool $strictPadding = false $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; if (($c_num0 | $c_alpha0) === 0) { - throw new \RangeException( + throw new RangeException( 'Expected hexadecimal character' ); } diff --git a/src/RFC4648.php b/src/RFC4648.php index 5ceda31..f124d65 100644 --- a/src/RFC4648.php +++ b/src/RFC4648.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace ParagonIE\ConstantTime; +use TypeError; + /** * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) @@ -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 @@ public static function base64Encode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base64Decode(string $str): string { @@ -69,7 +73,8 @@ public static function base64Decode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base64UrlSafeEncode(string $str): string { @@ -83,7 +88,8 @@ public static function base64UrlSafeEncode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base64UrlSafeDecode(string $str): string { @@ -97,7 +103,8 @@ public static function base64UrlSafeDecode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base32Encode(string $str): string { @@ -111,7 +118,8 @@ public static function base32Encode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base32Decode(string $str): string { @@ -125,7 +133,8 @@ public static function base32Decode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base32HexEncode(string $str): string { @@ -139,7 +148,8 @@ public static function base32HexEncode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base32HexDecode(string $str): string { @@ -153,7 +163,8 @@ public static function base32HexDecode(string $str): string * * @param string $str * @return string - * @throws \TypeError + * + * @throws TypeError */ public static function base16Encode(string $str): string {