Skip to content

Commit

Permalink
Remove class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 6, 2024
1 parent 6a2b933 commit d137eb0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 197 deletions.
144 changes: 0 additions & 144 deletions src/Base32/Base32.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Base32/Base32Exception.php

This file was deleted.

15 changes: 1 addition & 14 deletions src/Base32/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,7 @@ public function it_will_base32_encode_and_decode(string $string): void
#[Test]
public function it_will_return_false_from_invalid_encoded_string_with_base32_decode_function(string $sequence, string $message, string $encoding): void
{
self::assertFalse(base32_decode($sequence, $encoding, true, true));
}

#[DataProvider('invalidDecodingSequence')]
#[Test]
public function it_will_throw_from_invalid_encoded_string_with_base32_decode_method_on_strict_mode(string $sequence, string $message, string $encoding): void
{
$this->expectException(Base32Exception::class);
$this->expectExceptionMessage($message);

match ($encoding) {
PHP_BASE32_HEX => Base32::decode($sequence, PHP_BASE32_HEX, '=', true),
default => Base32::decode($sequence, PHP_BASE32_ASCII, '=', true),
};
self::assertFalse(base32_decode($sequence, $encoding, true));
}

/**
Expand Down
39 changes: 24 additions & 15 deletions src/Base32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,41 @@ You need:

The package provides a userland base32 encoding and decoding mechanism.

You can either use the `Base32` class as shown below:

```php
<?php
base32_encode(string $string, string $alphabet = PHP_BASE32_ASCII): string
base32_decode(string $string, string $alphabet = PHP_BASE32_ASCII, bool $strict = false): string
```

use Bakame\Aide\Base32\Base32;
#### Parameters:

Base32::encode('Bangui'); // returns 'IJQW4Z3VNE======'
Base32::decode('IJQW4Z3VNE======'); // returns 'Bangui'
Base32::decodeLax('IJQW4Z083VNE======'); // returns 'Bangui'
Base32::decode('IJQW4Z083VNE======'); // throws Base32Exception
Base32::encode('Bangui', Base32::HEX); // returns '89GMSPRLD4======'
Base32::decode('89GMSPRLD4======', Base32::HEX); // returns 'Bangui'
```
- `$string` : the data to encode for `base32_encode` or to decode for `base32_decode`
- `$alphabet` : the base32 alphabet, by default `PHP_BASE_ASCII`.

If `$alphabet` is `PHP_BASE_ASCII`, conversion is performed per RFC4648 US-ASCII standard.
If `$alphabet` is `PHP_BASE_HEXC`, conversion is performed per RFC4648 HEX standard.

**You can provide your own alphabet of 32 characters.**

- `$strict` : tell whether we need to perform strict decoding or not

If the strict parameter is set to `true` then the base32_decode() function will return `false`

- if encoded sequence lenght is invalid
- if the input contains character from outside the base64 alphabet.
- if padding is invalid
- if encoded characters are not all uppercased

or use the equivalent functions in the default scope
otherwise listed constraints are silently ignored or discarded.

```php
<?php

base32_encode('Bangui'); // returns 'IJQW4Z3VNE======'
base32_decode('IJQW4Z3VNE======'); // returns 'Bangui'
base32_decode('IJQW4Z083VNE======'); // returns 'Bangui'
base32_decode('IJQW4Z083VNE======', PHP_BASE32_ASCII, '=', true); // throws Base32Exception
base32_encode('Bangui', PHP_BASE32_HEX); // returns '89GMSPRLD4======'
base32_decode('89GMSPRLD4======', PHP_BASE32_HEX, '=', true); // returns 'Bangui'
base32_decode('IJQW4Z083VNE======', PHP_BASE32_ASCII, true); // return false
base32_encode('Bangui', PHP_BASE32_HEX); // returns '89GMSPRLD4======'
base32_decode('89GMSPRLD4======', PHP_BASE32_HEX, true); // returns 'Bangui'
```

In case of an error during decoding the `Base32` enumeration will throw a `Base23Exception` while
Expand Down
16 changes: 3 additions & 13 deletions src/Base32/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
function base32_encode(
string $decoded,
string $alphabet = PHP_BASE32_ASCII,
bool $usePadding = true
): string {
$padding = '';
if ($usePadding) {
$padding = '=';
}

if ('' === $decoded) {
return '';
}
Expand All @@ -27,6 +21,7 @@ function base32_encode(
$len = strlen($decoded);
$decoded .= str_repeat(chr(0), 4);
$chars = (array) unpack('C*', $decoded);
$padding = '=';
$alphabet .= $padding;

while ($n < $len || 0 !== $bitLen) {
Expand All @@ -50,14 +45,8 @@ function base32_encode(
function base32_decode(
string $encoded,
string $alphabet = PHP_BASE32_ASCII,
bool $usePadding = true,
bool $strict = false
): string|false {
$padding = '';
if ($usePadding) {
$padding = '=';
}

if ('' === $encoded) {
return '';
}
Expand All @@ -70,6 +59,7 @@ function base32_decode(
return false;
}

$padding = '=';
$remainder = strlen($encoded) % 8;
if (0 !== $remainder) {
if ($strict) {
Expand Down Expand Up @@ -97,7 +87,7 @@ function base32_decode(
$encoded = str_replace($padding, '', $inside).substr($encoded, strlen($inside));
}

if ($strict && '' !== $padding && 1 !== preg_match('/^[^'.$padding.']+(('.$padding.'){3,4}|('.$padding.'){6}|'.$padding.')?$/', $encoded)) {
if ($strict && 1 !== preg_match('/^[^'.$padding.']+(('.$padding.'){3,4}|('.$padding.'){6}|'.$padding.')?$/', $encoded)) {
return false;
}

Expand Down

0 comments on commit d137eb0

Please sign in to comment.