Base85 encoder and decoder for arbitrary data

Related tags

Miscellaneous base85
Overview

All your Base85

Latest Version Software License Build StatusCoverage

Install

Install with composer.

$ composer require tuupola/base85

This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.

$ composer require "tuupola/base85:^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.

$base85 = new Tuupola\Base85;

$encoded = $base85->encode(random_bytes(128));
$decoded = $base85->decode($encoded);

If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods.

$integer = $base85->encodeInteger(987654321); /* 3o4PT */
print $base85->decodeInteger("3o4PT", true); /* 987654321 */

Note that encoding a string and an integer will yield different results.

$string = $base85->encode("987654321"); /* 3B/rU2)I*E0` */
$integer = $base85->encodeInteger(987654321); /* 3o4PT */

Encoding modes

ASCII85 encoding. This is the default. 0x00000000 is compressed to z. Spaces are not compressed.

use Tuupola\Base85;

$ascii85 = new Base85([
    "characters" => Base85::ASCII85,
    "compress.spaces" => false,
    "compress.zeroes" => true
]);

print $ascii85->encode("Hello world!"); /* 87cURD]j7BEbo80 */

Adobe ASCII85 encoding is same as previous except data is enclosed between <~ and ~>.

use Tuupola\Base85;

$adobe85 = new Base85([
    "characters" => Base85::ASCII85,
    "compress.spaces" => false,
    "compress.zeroes" => true,
    "prefix" => "<~",
    "suffix" => "~>"
]);

print $adobe85->encode("Hello world!"); /* <~87cURD]j7BEbo80~> */

ZeroMQ (Z85) encoding. NOTE! Even though specification says input length must be divisible by 4, this is not currently enforced. Spaces and zeroes are not compressed.

use Tuupola\Base85;

$z85 = new Base85([
    "characters" => Base85::Z85,
    "compress.spaces" => false,
    "compress.zeroes" => false
]);

print $z85->encode("Hello world!"); /* NM=qnZy<MXa+]NF */

Character set from RFC1924 which is an April fools joke. Spaces and zeroes are not compressed.

use Tuupola\Base85;

$rfc1924 = new Base85([
    "characters" => Base85::RFC1924,
    "compress.spaces" => false,
    "compress.zeroes" => false
]);

print $rfc1924->encode("Hello world!"); /* NM&qnZy<MXa%^NF */

Speed

Pure PHP encoder seems to be faster than the GMP implementation. Below benchmarks use random_bytes(128) as data.

$ php --version
PHP 8.0.1 (cli) (built: Jan  8 2021 09:07:02) ( NTS )

$ vendor/bin/phpbench run benchmarks/ --report=default

+-----------------+-----------------+-------+
| subject         | mean            | diff  |
+-----------------+-----------------+-------+
| benchGmpDecoder | 24,515.692ops/s | 1.01x |
| benchPhpDecoder | 24,666.509ops/s | 1.00x |
+-----------------+-----------------+-------+
+-----------------+-----------------+-------+
| subject         | mean            | diff  |
+-----------------+-----------------+-------+
| benchGmpEncoder | 9,654.448ops/s  | 4.76x |
| benchPhpEncoder | 45,944.903ops/s | 1.00x |
+-----------------+-----------------+-------+

Static Proxy

If you prefer static syntax use the provided static proxy.

use Tuupola\Base85Proxy as Base85;

print Base85::encode("Hello world!") /* 87cURD]j7BEbo80 */

To change static proxy options set the Base85::$options variable.

use Tuupola\Base85;
use Tuupola\Base85Proxy as Z85;

Z85::$options = [
    "characters" => Base85::Z85,
    "compress.spaces" => false,
    "compress.zeroes" => false
];

print Z85::encode("Hello world!"); /* NM=qnZy<MXa+]NF */

Testing

You can run tests either manually or automatically on every code change. Automatic tests require entr to work.

$ make test
$ brew install entr
$ make watch

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

Comments
  • PHP 8.x support

    PHP 8.x support

    Hi,

    Any plans to upgrade this package for it to be used with PHP 8.x? I cloned the repository and tried to modify the composer.json dependencies but it's not that simple if I want to properly test the package (phpbench doesn't run on PHP 8 either) although it works with the same examples in the Usage section.

    Any change to publish an update? :)

    opened by jmecahansen 2
  • Test enhancement

    Test enhancement

    Changed log

    • add php-7.2 test.
    • remove bootstrap.php and use vendor/autoload.php directly.
    • use the class-based PHPUnit namespace.
    • set the different PHPUnit versions are for the different PHP versions.
    opened by peter279k 1
  • Wrong Z85 character set

    Wrong Z85 character set

    Currently the library uses

    0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-:+=^!/*?&<>()[]{}@%$#

    while it should be

    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#

    opened by tuupola 0
  • ASCII85. Four zero bytes encoded as !!!!! instead of z

    ASCII85. Four zero bytes encoded as !!!!! instead of z

    Four zero bytes encoded as !!!!! instead of z.

    $ascii85 = new Base85([
        "characters" => Base85::ASCII85,
        "compress.spaces" => false,
        "compress.zeroes" => true
    ]);
    print $ascii85->encode("\0\0\0\0"); // !!!!!
    

    I tested with https://cryptii.com/pipes/ascii85-encoding

    ascii85

    I tested with Python too. base64.a85encode outputs z for four zero bytes.

    opened by distlibs 3
  • Different versions of PHP output different results

    Different versions of PHP output different results

    I tested "Base85" on PHP 5.X and PHP 7.X. The result of PHP 5.X seems to be abnormal. To find out the cause of the problem, I tested more text. The conclusion is as follows:

    • Decoding all successful
    • Encoding in PHP 5.X is abnormal
    • English character encoding succeeded
    • Japanese and Chinese character encoding is abnormal
    • Invisible characters are generated, but the English part is normal

    The problem seems to be in unpack("N*", $data) ? I'm confused about it.

    opened by xmoer 2
Owner
Mika Tuupola
Mika Tuupola
DiscordLookup | Get more out of Discord with Discord Lookup! Snowflake Decoder, Guild List with Stats, Invite Info and more...

DiscordLookup Get more out of Discord with Discord Lookup! Snowflake Decoder, Guild List with Stats, Invite Info and more... Website Getting Help Tool

Felix 69 Dec 23, 2022
Okex API Like the official document interface, Support for arbitrary extension.

It is recommended that you read the official document first Okex docs https://www.okex.com/docs/en Okex Simulation Test API https://www.okex.com/docs/

lin 34 Jan 1, 2023
Smd thumbnail - Multiple image thumbnails of arbitrary dimensions

smd_thumbnail Download | Packagist If you’re bored of one Textpattern thumbnail per image and don’t fancy using an auto-resizing script or relying on

Stef Dawson 9 Dec 9, 2021
Melek Berita Backend is a service for crawling data from various websites and processing the data to be used for news data needs.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Chacha Nurholis 2 Oct 9, 2022
Import data from and export data to a range of different file formats and media

Ddeboer Data Import library This library has been renamed to PortPHP and will be deprecated. Please use PortPHP instead. Introduction This PHP library

David de Boer 570 Dec 27, 2022
Data visualization for NASA's DSNNow public data

DSN Monitor Data visualization for NASA's DSNNow public data. A live version of the project can be accessed at http://dsnmonitor.ddns.net. Description

Vinz 2 Sep 18, 2022
:globe_with_meridians: List of all countries with names and ISO 3166-1 codes in all languages and data formats.

symfony upgrade fixer • twig gettext extractor • wisdom • centipede • permissions handler • extraload • gravatar • locurro • country list • transliter

Saša Stamenković 5k Dec 22, 2022
Get mobile app version and other related data from Google Play Store, Apple App Store and Huawei AppGallery

Mobile App Version Get mobile app version and other related data from Google Play Store, Apple App Store and Huawei AppGallery. Installation Add to co

Omer Salaj 11 Mar 15, 2022
JSON schema models and generated code to validate and handle various data in PocketMine-MP

DataModels JSON schema models and generated code to validate and handle various data in PocketMine-MP This library uses php-json-schema-model-generato

PMMP 2 Nov 9, 2022
Silverstripe-searchable - Adds to the default Silverstripe search by adding a custom results controller and allowing properly adding custom data objects and custom fields for searching

SilverStripe Searchable Module UPDATE - Full Text Search This module now uses Full Text Support for MySQL/MariaDB databases in version 3.* Adds more c

ilateral 13 Apr 14, 2022
Project that aims to create a website for a gym, where the clients and employees can access their data, buy in the gym store and check the gym activities.

Gym_Management_Project Project that aims to create a website for a gym, where the clients and employees can access their data, buy in the gym store an

null 1 Jan 12, 2022
Scalable and durable data imports for publishing and consuming APIs

Porter Scalable and durable data imports for publishing and consuming APIs Porter is the all-purpose PHP data importer. She fetches data from anywhere

null 596 Jan 6, 2023
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

EcomDev B.V. 51 Dec 5, 2022
MailChimp for Magento 2. Syncs all data (subscriber, customers, orders, products) and enables marketing automation with email campaigns, automations, ads, postcards and more.

MailChimp for Magento 2. Syncs all data (subscriber, customers, orders, products) and enables marketing automation with email campaigns, automations, ads, postcards and more.

Mailchimp 139 Sep 9, 2022
A PHP spreadsheet reader (Excel XLS and XLSX, OpenOffice ODS, and variously separated text files) with a singular goal of getting the data out, efficiently

spreadsheet-reader is a PHP spreadsheet reader that differs from others in that the main goal for it was efficient data extraction that could handle l

Nuovo 666 Dec 24, 2022
Enhance product data quality and streamline content creation with the Pimcore and ChatGPT integration.

chatgpt-pimcore Enhance product data quality and streamline content creation with the Pimcore and ChatGPT integration. Overview The integration of Pim

Pravin chaudhary 6 Jun 5, 2023
Dobren Dragojević 6 Jun 11, 2023
RRR makes structured data for WordPress really rich, and really easy.

Really Rich Results - JSON-LD Structured Data (Google Rich Results) for WordPress Search engines are putting more weight on structured data than ever

Pagely 22 Dec 1, 2022
Library download currency rate and save in database, It's designed to be extended by any available data source.

Library download currency rate and save in database, It's designed to be extended by any available data source.

Flexmind. Krzysztof Bielecki 2 Oct 6, 2021