Download the PHP package mfonte/base62x without Composer
On this page you can find all versions of the php package mfonte/base62x. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mfonte/base62x
More information about mfonte/base62x
Files in mfonte/base62x
Package base62x
Short Description A Base62x PHP implementation library, that can be used in frameworks like Laravel, that supports encoding/decoding of strings, arrays, objects, and binary resources. It also supports Gzip compression/decompression and symmetric encryption.
License Apache-2.0
Rated 2.33 based on 3 reviews
Informations about the package base62x
PHP Base62x Library
This library can be used to encode strings in Base62x format.
The reference implementation of Base62x has been taken by this repository: https://github.com/wadelau/Base62x
Stating the original author repository, Base62x is an alternative approach to Base 64 without symbols in output.
Base62x is an non-symbolic Base64 encoding scheme. It can be used safely in computer file systems, programming languages for data exchange, internet communication systems, etc, and it is an ideal substitute and successor of many variants of Base64 encoding scheme.
This repository is a wrapper around wadelau/Base62x repository, and is specifically crafted for PHP, with composer support.
It can be integrated into any framework, like Laravel, to enable Base62x support out of the box.
Installation
Simple enough.
composer require mfonte/base62x
Required environment:
- PHP >= 7.2
gzip
module, for gzip compression supportopenssl
module, for encryption support
Use cases
Base64 is notoriously bad to be used in GET strings. With this library, you can safely encode (and compress) and pass your data via GET parameters.
Another use case is when you have to communicate binary data from one server to another.
Many Apache modules (like Mod Security) will do some strange things, already seen in the wild, while passing raw binary data or complex jugglary in server2server communication.
Sure, you can use native base64_encode()
and base64_decode()
on both ends, but using this library not only you're encoding data, you can also compress and encrypt it on the go!.
Basic Usage
Usage is simple enough with a nice, expressive API:
Real use case scenario
A useful example of real use case scenario, that I've used on some projects, is passing json context data from an HTML page to an Ajax worker.
The data lifecycle from the frontend to the Ajax backend is fully encrypted, and doesn't break HTML apart, as Base62x strings are based on an alphabet that is compatible with embedding them in plain JS strings like I've shown in the provided example.
In the Ajax backend you can validate the token, collect the context data, and reply only if the setup is correct:
The library supports binary streams
It means that you can encode binary data, for example raw JPEGs or raw audio files.
The library supports compressed Base62x encoding!
While encoding in Base62x, you can also compress the payload.
This is expecially useful if you're planning on sending huge arrays or huge data via GET or POST requests, that you need to be sure it would not trigger errors from one machine to another, or from one url to another.
Important note: while decoding and decompressing, you don't need to specify the original compression algorithm, because this information is saved in a "magic string" at the beginning of the encoded payload.
So, if you don't have control over another machine where your payloads can end, you don't have to worry to communicate the other side what type of compression you're actually using. The decode()
method will automagically take care of that.
Available compression methods
- Gzip, encoding
zlib
,deflate
, orgzip
. Without any further instruction, thecompress()
method defaults togzip/zlib
- Huffman via a dedicated PHP Huffman implementation. This compression method is currently not working. Please, do not use it in production environments.
These parameters can be passed to the compress()
method like so:
Encryption
As of version 1.2 this library support encryption.
The encryption can be chained with compression.
Testing
Simply run composer install
over this module's installation directory.
Then, run composer test
to run all the tests.
Some tests may fail as testEncodingWithAllAvailableEncryptionAlgorithms performs a check over all available openssl_get_cipher_methods() installed on your environment. A possible example of failure is Encryption method "id-aes128-CCM" is either unsupported in your PHP installation or not a valid encryption algorithm.
TODO
- [ ] Fix Huffman compression
- [ ] Add Bzip2 compression
Contributing
If you want to contribute to this project, please use php-cs-fixer to format your code to PSR standards and rules
specified in the configuration file .php-cs-fixer.dist.php
provided in this repository.
Thank you!
Thank you's
A big thank you goes to wadelau [Wade Lau at ufqi.com] for implementing such an useful encoding pattern.