functions or class to allow encoding or decoding strings using RFC4648 base32 algorithm.
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|false$decoded: the string to encode forbase32_encode$encoded: the string to decode forbase32_decode$alphabet: the base32 alphabet, by defaultPHP_BASE32_ASCII.$padding: the 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 lettering.
otherwise listed constraints are silently ignored or discarded.
- If
$alphabetisPHP_BASE32_ASCIIand the$paddingis=, conversion is performed per RFC4648 US-ASCII standard. - If
$alphabetisPHP_BASE32_HEXand the$paddingis=, conversion is performed per RFC4648 HEX standard.
You MAY provide your own alphabet of 32 characters and your own padding character.
<?php
base32_encode('Bangui'); // returns 'IJQW4Z3VNE======'
base32_decode('IJQW4Z3VNE======'); // returns 'Bangui'
base32_decode('IJQW4Z083VNE======'); // returns 'Bangui'
base32_decode(encoded:'IJQW4Z083VNE======', strict: true) // return false
base32_encode('Bangui', PHP_BASE32_HEX, '*'); // returns '89GMSPRLD4******'
base32_decode('89GMSPRLD4******', PHP_BASE32_HEX, '*'); // returns 'Bangui'