Base32
This library implements Base32 encoding. In addition to integers it can encode and decode any arbitrary data., (*1)
, (*2)
Install
Install with composer., (*3)
``` bash
$ composer require tuupola/base32, (*4)
This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.
``` bash
$ composer require "tuupola/base32:^1.0"
Usage
This package has both pure PHP and GMP based encoders. By default encoder and decoder will use GMP functions if the extension is installed. If GMP is not available pure PHP encoder will be used instead., (*5)
``` php
$base32 = new Tuupola\Base32;, (*6)
$encoded = $base32->encode(random_bytes(128));
$decoded = $base32->decode($encoded);, (*7)
If you are encoding to and from integer use the implicit `decodeInteger()` and `encodeInteger()` methods.
``` php
$integer = $base32->encodeInteger(987654321); /* 5N42FR== */
print $base32->decodeInteger("5N42FR=="); /* 987654321 */
Also note that encoding a string and an integer will yield different results., (*8)
``` php
$integer = $base32->encodeInteger(987654321); /* 5N42FR== /
$string = $base32->encode("987654321"); / FHE4DONRVGQZTEMI= */, (*9)
## Encoding Modes
### RCF4684
[RCF4684](https://tools.ietf.org/html/rfc4648) is the default when encoder is created without passing any parameters. Explicit parameters shown below.
```php
use Tuupola\Base32;
$base32 = new Base32([
"characters" => Base32::RFC4648,
"padding" => "="
]);
print $base32->encode("Hello world!"); /* JBSWY3DPEB3W64TMMQQQ==== */
RCF4684 HEX
RCF4684 base32hex encoding is identical to previous, except for the character set. This encoding is lexically sortable., (*10)
$base32hex = new Base32([
"characters" => Base32::HEX,
"padding" => "="
]);
print $base32->encode("Hello world!"); /* 91IMOR3F41RMUSJCCGGG==== */
GMP
GMP encoding is identical to previous. Example below is shown with padding disabled., (*11)
$gmp = new Base32([
"characters" => Base32::GMP,
"padding" => false
]);
print $gmp->encode("Hello world!"); /* 91IMOR3F41RMUSJCCGGG */
Crockford's Base32
When decoding, upper and lower case letters are accepted, and i and l will be treated as 1 and o will be treated as 0. When encoding, only upper case letters are used. Hyphens are ignored during decoding., (*12)
$crockford = new Base32([
"characters" => Base32::CROCKFORD,
"padding" => false,
"crockford" => true,
]);
print $crockford->encode("Hello world!"); /* 91JPRV3F41VPYWKCCGGG */
print $crockford->decode("91JPRV3F41VPYWKCCGGG"); /* Hello world! */
print $crockford->decode("91jprv3f41vpywkccggg"); /* Hello world! */
print $crockford->decode("9ljprv3f4lvpywkccggg"); /* Hello world! */
Z-base-32 [WIP]
http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt, (*13)
Character Sets
By default Base32 uses RFC4648 character set. Shortcut is provided for other commonly used character sets. You can also use any custom character set of 32 unique characters., (*14)
use Tuupola\Base32;
print Base32::CROCKFORD; /* 0123456789ABCDEFGHJKMNPQRSTVWXYZ */
print Base32::RFC4648; /* ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
print Base32::ZBASE32; /* ybndrfg8ejkmcpqxot1uwisza345h769 */
print Base32::GMP; /* 0123456789ABCDEFGHIJKLMNOPQRSTUV */
print Base32::HEX; /* 0123456789ABCDEFGHIJKLMNOPQRSTUV */
$default = new Base32(["characters" => Base32::RFC4648]);
$crockford = new Base32(["characters" => Base32::CROCKFORD]);
print $default->encode("Hello world!"); /* JBSWY3DPEB3W64TMMQQQ==== */
print $inverted->encode("Hello world!"); /* 91JPRV3F41VPYWKCCGGG==== */
Speed
Install GMP if you can. It is reasonably faster pure PHP encoder. Below benchmarks are for encoding random_bytes(128)
data., (*15)
$ make benchmark
benchmark: Base32Bench
+-----------------------+----------------+-------+
| subject | mean | diff |
+-----------------------+----------------+-------+
| benchGmpEncoder | 8,361.204ops/s | 1.00x |
| benchGmpEncoderCustom | 8,393.487ops/s | 1.00x |
| benchPhpEncoder | 6,881.365ops/s | 1.22x |
+-----------------------+----------------+-------+
Static Proxy
If you prefer to use static syntax use the provided static proxy., (*16)
``` php
use Tuupola\Base32Proxy as Base32;, (*17)
$encoded = Base32::encode(random_bytes(128));
$decoded = Base32::decode($encoded);, (*18)
## Testing
You can run tests either manually or automatically on every code change. Automatic tests require [entr](http://entrproject.org/) to work.
``` bash
$ make test
bash
$ brew install entr
$ make watch
, (*19)
Contributing
Please see CONTRIBUTING for details., (*20)
Security
If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker., (*21)
License
The MIT License (MIT). Please see License File for more information., (*22)