Introduction
The base encoding validators allow the user to validate base64, -32 and -16
encoded messages.
The validator will test if the string is encoded in the requested encoding., (*1)
Validators will validate against the specification of
RFC 4648., (*2)
Additionally, the Base32 Crockford
implementation is supported., (*3)
Installation
composer require hylianshield/validator-base-encoding:^2.0
Usage
The encoding validators can be configured to require padding or make it optional.
Additionally, some implementations require that partitioning will be supported., (*4)
The RFC section on the interpretation of non-alphabet characters
states:, (*5)
Implementations MUST reject the encoded data if it contains
characters outside the base alphabet when interpreting base-encoded
data, unless the specification referring to this document explicitly
states otherwise. Such specifications may instead state, as MIME
does, that characters outside the base encoding alphabet should
simply be ignored when interpreting data ("be liberal in what you
accept"). Note that this means that any adjacent carriage return/
line feed (CRLF) characters constitute "non-alphabet characters" and
are ignored., (*6)
Therefore, the constructors of the validators have the following signature:, (*7)
bool $requirePadding = true,
bool $allowPartitioning = false
With the exception of the Base16Validator
, which does not use padding and
therefore omits the first parameter, as well as the Base32CrockfordValidator
,
which requires padding and as such omits it as well., (*8)
Padding validation
<?php
use HylianShield\Validator\BaseEncoding\Base64Validator;
// By default, padding is required.
$validator = new Base64Validator();
$validator->validate('d2Fycmlvcg=='); // true
$validator->validate('d2Fycmlvcg'); // false
// One can make padding validation optional.
$validator = new Base64Validator(false);
$validator->validate('d2Fycmlvcg=='); // true
$validator->validate('d2Fycmlvcg'); // true
CRLF validation
<?php
use HylianShield\Validator\BaseEncoding\Base64Validator;
// By default, partitioning is disallowed.
$validator = new Base64Validator();
$validator->validate('d2Fycmlvcg=='); // true
$validator->validate("d2Fycm\r\nlvcg=="); // false
// One can make partitioning allowed like so:
$validator = new Base64Validator(true, true);
$validator->validate('d2Fycmlvcg=='); // true
$validator->validate("d2Fycm\r\nlvcg=="); // true
Supported encodings
Validators are defined in the \HylianShield\Validator\BaseEncoding
namespace., (*9)
Base 64 encoding
Attribute |
Value |
Name |
base64 |
Specification |
RFC 4648 - Section 4 |
Padding |
= (optional) |
Partitioning |
\r\n (optional) |
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base64Validator;
new Base64Validator(
$requirePadding = true,
$allowPartitions = false
);
Base 64 Encoding with URL and Filename Safe Alphabet
Attribute |
Value |
Name |
base64url |
Specification |
RFC 4648 - Section 5 |
Padding |
= (optional) |
Partitioning |
\r\n (optional) |
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base64UrlValidator;
new Base64UrlValidator(
$requirePadding = true,
$allowPartitions = false
);
Base 32 encoding
Attribute |
Value |
Name |
base32 |
Specification |
RFC 4648 - Section 6 |
Padding |
= (optional) |
Partitioning |
\r\n (optional) |
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base32Validator;
new Base32Validator(
$requirePadding = true,
$allowPartitions = false
);
Base 32 Encoding with Extended Hex Alphabet
Attribute |
Value |
Name |
base32hex |
Specification |
RFC 4648 - Section 7 |
Padding |
= (optional) |
Partitioning |
\r\n (optional) |
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base32HexValidator;
new Base32HexValidator(
$requirePadding = true,
$allowPartitions = false
);
Crockford's Base 32
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base32CrockfordValidator;
new Base32CrockfordValidator(
$allowPartitions = true
);
Base 16 encoding
Attribute |
Value |
Name |
base16 |
Specification |
RFC 4648 - Section 8 |
Padding |
No padding |
Partitioning |
\r\n (optional) |
Signature
<?php
use HylianShield\Validator\BaseEncoding\Base16Validator;
new Base16Validator(
$allowPartitions = false
);