Skip to content

Commit 58c3f47

Browse files
Boyscouting
1 parent 817e88a commit 58c3f47

File tree

6 files changed

+79
-47
lines changed

6 files changed

+79
-47
lines changed

src/Base32.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66
use RangeException;
7+
use TypeError;
78

89
/**
910
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
@@ -65,7 +66,7 @@ public static function decodeUpper(string $src, bool $strictPadding = false): st
6566
*
6667
* @param string $binString
6768
* @return string
68-
* @throws \TypeError
69+
* @throws TypeError
6970
*/
7071
public static function encode(string $binString): string
7172
{
@@ -76,7 +77,7 @@ public static function encode(string $binString): string
7677
*
7778
* @param string $src
7879
* @return string
79-
* @throws \TypeError
80+
* @throws TypeError
8081
*/
8182
public static function encodeUnpadded(string $src): string
8283
{
@@ -88,7 +89,7 @@ public static function encodeUnpadded(string $src): string
8889
*
8990
* @param string $src
9091
* @return string
91-
* @throws \TypeError
92+
* @throws TypeError
9293
*/
9394
public static function encodeUpper(string $src): string
9495
{
@@ -100,7 +101,7 @@ public static function encodeUpper(string $src): string
100101
*
101102
* @param string $src
102103
* @return string
103-
* @throws \TypeError
104+
* @throws TypeError
104105
*/
105106
public static function encodeUpperUnpadded(string $src): string
106107
{
@@ -187,6 +188,7 @@ protected static function encode5BitsUpper(int $src): string
187188

188189
/**
189190
* @param string $encodedString
191+
* @param bool $upper
190192
* @return string
191193
*/
192194
public static function decodeNoPadding(string $encodedString, bool $upper = false): string
@@ -218,11 +220,15 @@ public static function decodeNoPadding(string $encodedString, bool $upper = fals
218220
* @param bool $upper
219221
* @param bool $strictPadding
220222
* @return string
221-
* @throws \TypeError
223+
*
224+
* @throws TypeError
222225
* @psalm-suppress RedundantCondition
223226
*/
224-
protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
225-
{
227+
protected static function doDecode(
228+
string $src,
229+
bool $upper = false,
230+
bool $strictPadding = false
231+
): string {
226232
// We do this to reduce code duplication:
227233
$method = $upper
228234
? 'decode5BitsUpper'
@@ -244,7 +250,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
244250
}
245251
}
246252
if (($srcLen & 7) === 1) {
247-
throw new \RangeException(
253+
throw new RangeException(
248254
'Incorrect padding'
249255
);
250256
}
@@ -426,7 +432,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
426432
* @param bool $upper
427433
* @param bool $pad
428434
* @return string
429-
* @throws \TypeError
435+
* @throws TypeError
430436
*/
431437
protected static function doEncode(string $src, bool $upper = false, $pad = true): string
432438
{

src/Base64.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66
use RangeException;
7+
use TypeError;
78

89
/**
910
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
@@ -43,7 +44,8 @@ abstract class Base64 implements EncoderInterface
4344
*
4445
* @param string $binString
4546
* @return string
46-
* @throws \TypeError
47+
*
48+
* @throws TypeError
4749
*/
4850
public static function encode(string $binString): string
4951
{
@@ -57,7 +59,8 @@ public static function encode(string $binString): string
5759
*
5860
* @param string $src
5961
* @return string
60-
* @throws \TypeError
62+
*
63+
* @throws TypeError
6164
*/
6265
public static function encodeUnpadded(string $src): string
6366
{
@@ -68,7 +71,8 @@ public static function encodeUnpadded(string $src): string
6871
* @param string $src
6972
* @param bool $pad Include = padding?
7073
* @return string
71-
* @throws \TypeError
74+
*
75+
* @throws TypeError
7276
*/
7377
protected static function doEncode(string $src, bool $pad = true): string
7478
{
@@ -122,8 +126,9 @@ protected static function doEncode(string $src, bool $pad = true): string
122126
* @param string $encodedString
123127
* @param bool $strictPadding
124128
* @return string
125-
* @throws \RangeException
126-
* @throws \TypeError
129+
*
130+
* @throws RangeException
131+
* @throws TypeError
127132
* @psalm-suppress RedundantCondition
128133
*/
129134
public static function decode(string $encodedString, bool $strictPadding = false): string

src/Binary.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
declare(strict_types=1);
33
namespace ParagonIE\ConstantTime;
44

5+
use TypeError;
6+
57
/**
68
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
79
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
@@ -64,7 +66,8 @@ public static function safeStrlen(string $str): int
6466
* @param int $start
6567
* @param ?int $length
6668
* @return string
67-
* @throws \TypeError
69+
*
70+
* @throws TypeError
6871
*/
6972
public static function safeSubstr(
7073
string $str,

src/Encoding.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
declare(strict_types=1);
33
namespace ParagonIE\ConstantTime;
44

5+
use TypeError;
6+
57
/**
68
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
79
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
@@ -36,7 +38,7 @@ abstract class Encoding
3638
*
3739
* @param string $str
3840
* @return string
39-
* @throws \TypeError
41+
* @throws TypeError
4042
*/
4143
public static function base32Encode(string $str): string
4244
{
@@ -48,7 +50,7 @@ public static function base32Encode(string $str): string
4850
*
4951
* @param string $str
5052
* @return string
51-
* @throws \TypeError
53+
* @throws TypeError
5254
*/
5355
public static function base32EncodeUpper(string $str): string
5456
{
@@ -60,7 +62,7 @@ public static function base32EncodeUpper(string $str): string
6062
*
6163
* @param string $str
6264
* @return string
63-
* @throws \TypeError
65+
* @throws TypeError
6466
*/
6567
public static function base32Decode(string $str): string
6668
{
@@ -72,7 +74,7 @@ public static function base32Decode(string $str): string
7274
*
7375
* @param string $str
7476
* @return string
75-
* @throws \TypeError
77+
* @throws TypeError
7678
*/
7779
public static function base32DecodeUpper(string $str): string
7880
{
@@ -84,7 +86,7 @@ public static function base32DecodeUpper(string $str): string
8486
*
8587
* @param string $str
8688
* @return string
87-
* @throws \TypeError
89+
* @throws TypeError
8890
*/
8991
public static function base32HexEncode(string $str): string
9092
{
@@ -96,7 +98,7 @@ public static function base32HexEncode(string $str): string
9698
*
9799
* @param string $str
98100
* @return string
99-
* @throws \TypeError
101+
* @throws TypeError
100102
*/
101103
public static function base32HexEncodeUpper(string $str): string
102104
{
@@ -108,7 +110,7 @@ public static function base32HexEncodeUpper(string $str): string
108110
*
109111
* @param string $str
110112
* @return string
111-
* @throws \TypeError
113+
* @throws TypeError
112114
*/
113115
public static function base32HexDecode(string $str): string
114116
{
@@ -120,7 +122,7 @@ public static function base32HexDecode(string $str): string
120122
*
121123
* @param string $str
122124
* @return string
123-
* @throws \TypeError
125+
* @throws TypeError
124126
*/
125127
public static function base32HexDecodeUpper(string $str): string
126128
{
@@ -132,7 +134,7 @@ public static function base32HexDecodeUpper(string $str): string
132134
*
133135
* @param string $str
134136
* @return string
135-
* @throws \TypeError
137+
* @throws TypeError
136138
*/
137139
public static function base64Encode(string $str): string
138140
{
@@ -144,7 +146,7 @@ public static function base64Encode(string $str): string
144146
*
145147
* @param string $str
146148
* @return string
147-
* @throws \TypeError
149+
* @throws TypeError
148150
*/
149151
public static function base64Decode(string $str): string
150152
{
@@ -157,7 +159,7 @@ public static function base64Decode(string $str): string
157159
* Base64 character set "./[A-Z][a-z][0-9]"
158160
* @param string $str
159161
* @return string
160-
* @throws \TypeError
162+
* @throws TypeError
161163
*/
162164
public static function base64EncodeDotSlash(string $str): string
163165
{
@@ -172,7 +174,7 @@ public static function base64EncodeDotSlash(string $str): string
172174
* @param string $str
173175
* @return string
174176
* @throws \RangeException
175-
* @throws \TypeError
177+
* @throws TypeError
176178
*/
177179
public static function base64DecodeDotSlash(string $str): string
178180
{
@@ -185,7 +187,7 @@ public static function base64DecodeDotSlash(string $str): string
185187
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
186188
* @param string $str
187189
* @return string
188-
* @throws \TypeError
190+
* @throws TypeError
189191
*/
190192
public static function base64EncodeDotSlashOrdered(string $str): string
191193
{
@@ -200,7 +202,7 @@ public static function base64EncodeDotSlashOrdered(string $str): string
200202
* @param string $str
201203
* @return string
202204
* @throws \RangeException
203-
* @throws \TypeError
205+
* @throws TypeError
204206
*/
205207
public static function base64DecodeDotSlashOrdered(string $str): string
206208
{
@@ -213,7 +215,7 @@ public static function base64DecodeDotSlashOrdered(string $str): string
213215
*
214216
* @param string $bin_string (raw binary)
215217
* @return string
216-
* @throws \TypeError
218+
* @throws TypeError
217219
*/
218220
public static function hexEncode(string $bin_string): string
219221
{
@@ -239,7 +241,7 @@ public static function hexDecode(string $hex_string): string
239241
*
240242
* @param string $bin_string (raw binary)
241243
* @return string
242-
* @throws \TypeError
244+
* @throws TypeError
243245
*/
244246
public static function hexEncodeUpper(string $bin_string): string
245247
{

src/Hex.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
declare(strict_types=1);
33
namespace ParagonIE\ConstantTime;
44

5+
use RangeException;
6+
use TypeError;
7+
58
/**
69
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
710
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
@@ -37,7 +40,7 @@ abstract class Hex implements EncoderInterface
3740
*
3841
* @param string $binString (raw binary)
3942
* @return string
40-
* @throws \TypeError
43+
* @throws TypeError
4144
*/
4245
public static function encode(string $binString): string
4346
{
@@ -64,7 +67,7 @@ public static function encode(string $binString): string
6467
*
6568
* @param string $binString (raw binary)
6669
* @return string
67-
* @throws \TypeError
70+
* @throws TypeError
6871
*/
6972
public static function encodeUpper(string $binString): string
7073
{
@@ -93,18 +96,20 @@ public static function encodeUpper(string $binString): string
9396
* @param string $encodedString
9497
* @param bool $strictPadding
9598
* @return string (raw binary)
96-
* @throws \RangeException
99+
* @throws RangeException
97100
*/
98-
public static function decode(string $encodedString, bool $strictPadding = false): string
99-
{
101+
public static function decode(
102+
string $encodedString,
103+
bool $strictPadding = false
104+
): string {
100105
$hex_pos = 0;
101106
$bin = '';
102107
$c_acc = 0;
103108
$hex_len = Binary::safeStrlen($encodedString);
104109
$state = 0;
105110
if (($hex_len & 1) !== 0) {
106111
if ($strictPadding) {
107-
throw new \RangeException(
112+
throw new RangeException(
108113
'Expected an even number of hexadecimal characters'
109114
);
110115
} else {
@@ -124,7 +129,7 @@ public static function decode(string $encodedString, bool $strictPadding = false
124129
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
125130

126131
if (($c_num0 | $c_alpha0) === 0) {
127-
throw new \RangeException(
132+
throw new RangeException(
128133
'Expected hexadecimal character'
129134
);
130135
}

0 commit comments

Comments
 (0)