Skip to content

Commit

Permalink
Improve base32 package implementation by removing complex function usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 25, 2024
1 parent 6f81dbe commit 59d4368
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class Base32
{
private const ALPHABET_SIZE = 32;
private const INVALID_CHARACTERS = "\r\n ";
private const RESERVED_CHARACTERS = "\r\n ";
/** @var non-empty-string */
private readonly string $alphabet;
/** @var non-empty-string */
Expand All @@ -22,22 +22,22 @@ final class Base32
*/
private function __construct(string $alphabet, string $padding)
{
if (1 !== strlen($padding) || false !== strpos(self::INVALID_CHARACTERS, $padding)) {
throw new ValueError('The padding character must be a valid single character.');
if (1 !== strlen($padding) || false !== strpos(self::RESERVED_CHARACTERS, $padding)) {
throw new ValueError('The padding character must be a non reserved single character.');
}

if (self::ALPHABET_SIZE !== strlen($alphabet)) {
throw new ValueError('The alphabet must be a 32 bytes long string.');
throw new ValueError('The alphabet must be a '.self::ALPHABET_SIZE.' bytes long string.');
}

$upperAlphabet = strtoupper($alphabet);
$upperPadding = strtoupper($padding);
if (32 !== strcspn($upperAlphabet, self::INVALID_CHARACTERS.$upperPadding)) {
throw new ValueError('The alphabet can not contain an invalid character.');
if (32 !== strcspn($upperAlphabet, self::RESERVED_CHARACTERS.$upperPadding)) {
throw new ValueError('The alphabet can not contain a reerved character.');
}

$uniqueChars = [];
for ($index = 0; $index < 32; $index++) {
for ($index = 0; $index < self::ALPHABET_SIZE; $index++) {
$char = $upperAlphabet[$index];
if (array_key_exists($char, $uniqueChars)) {
throw new ValueError('The alphabet must only contain unique characters.');
Expand Down Expand Up @@ -67,7 +67,7 @@ public function decode(string $encoded, bool $strict = false): string

$alphabet = $this->alphabet;
$padding = $this->padding;
$encoded = str_replace(str_split(self::INVALID_CHARACTERS), [''], $encoded);
$encoded = str_replace(str_split(self::RESERVED_CHARACTERS), [''], $encoded);
if (!$strict) {
$alphabet = strtoupper($alphabet);
$padding = strtoupper($padding);
Expand All @@ -92,7 +92,7 @@ public function decode(string $encoded, bool $strict = false): string

if (false !== strpos($inside, $padding)) {
if ($strict) {
throw new RuntimeException('The padding character is used inside the encoded data in an invalid place.');
throw new RuntimeException('The padding character is used in the encoded data in an invalid place.');
}

$encoded = str_replace($padding, '', $inside).$end;
Expand Down
14 changes: 7 additions & 7 deletions src/Base32/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,49 +236,49 @@ public static function invalidDecodingSequence(): iterable

yield 'the padding character is contained within the alphabet' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain an invalid character.',
'message' => 'The alphabet can not contain a reerved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => '*',
];

yield 'the padding character is contained within the alphabet is case insensitive' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain an invalid character.',
'message' => 'The alphabet can not contain a reerved character.',
'alphabet' => str_replace('A', '*', PHP_BASE32_ASCII),
'padding' => 'a',
];

yield 'the padding character is different than one byte' => [
'sequence' => 'A',
'message' => 'The padding character must be a valid single character.',
'message' => 'The padding character must be a non reserved single character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => 'yo',
];

yield 'the padding character can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The padding character must be a valid single character.',
'message' => 'The padding character must be a non reserved single character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\r",
];

yield 'the padding character can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The padding character must be a valid single character.',
'message' => 'The padding character must be a non reserved single character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\n",
];

yield 'the alphabet can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain an invalid character.',
'message' => 'The alphabet can not contain a reerved character.',
'alphabet' => substr(PHP_BASE32_ASCII, 0, -1)."\r",
'padding' => '=',
];

yield 'the alphabet can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain an invalid character.',
'message' => 'The alphabet can not contain a reerved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\n",
'padding' => '=',
];
Expand Down

0 comments on commit 59d4368

Please sign in to comment.