functions or class to allow encoding or decoding strings using RFC4648 base32 algorithm.
Caution
Sub-split of Aide for Base32.
composer require bakame-php/aide-base32
You need:
- PHP >= 8.1 but the latest stable version of PHP is recommended
The package provides a userland base32 encoding and decoding mechanism.
base32_encode(string $decoded, string $alphabet = PHP_BASE32_ASCII, $padding = '='): string
base32_decode(string $encoded, string $alphabet = PHP_BASE32_ASCII, $padding = '=', bool $strict = false): string
$decoded
: the data to encode forbase32_encode
$encoded
: the data to decode forbase32_decode
$alphabet
: the base32 alphabet, by defaultPHP_BASE32_ASCII
.$padding
: the padding character
If $alphabet
is PHP_BASE32_ASCII
and the $padding
is =
, conversion is performed per RFC4648 US-ASCII standard.
If $alphabet
is PHP_BASE32_HEXC
and the $padding
is =
, conversion is performed per RFC4648 HEX standard.
You can provide your own alphabet of 32 characters and your own padding character.
$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 length is invalid
- if the input contains character from outside the base64 alphabet.
- if padding is invalid
- if encoded characters do not follow the alphabet lettercase.
otherwise listed constraints are silently ignored or discarded.
<?php
base32_encode('Bangui'); // returns 'IJQW4Z3VNE======'
base32_decode('IJQW4Z3VNE======'); // returns 'Bangui'
base32_decode('IJQW4Z083VNE======'); // 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'