oire/base64

This package is abandoned and no longer maintained. The author suggests using the oire/iridium package instead.

Url-and filename-safe Base64 handling.

v2.2.1 2021-01-17 23:32 UTC

This package is not auto-updated.

Last update: 2021-03-07 23:26:15 UTC


README

Note! This library is not maintained anymore.

Please use Iridium security library instead.

Latest Version on Packagist MIT License

Encodes data to Base64 URL-safe way and decodes encoded data.

Requirements

This library requires PHP 7.3 or above.

Installation

Install via Composer:

composer require oire/base64

Running Tests

Run ./vendor/bin/phpunit in the project directory.

Compatibility with Earlier Versions of PHP

If you want a version compatible with PHP 7.1.2, please install version 1 instead:

composer require "oire/base64 ^1"

Usage Examples

use Oire\Base64\Base64;
use Oire\Base64\Exception\Base64Exception;

$text = "The quick brown fox jumps over the lazy dog";
$encoded = Base64::encode($text);
echo $encoded.PHP_EOL;

This will output:

VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw

By default, the encode() method truncates padding = signs as PHP’s built-in decoder handles this correctly. However, if the second parameter is given and set to true, = signs will be replaced with tildes (~), i.e.:

$encoded = Base64::encode($text, true);
echo $encoded.PHP_EOL;

This will output:

VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw~~

To decode the data, simply call Base64::decode():

$encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";

try {
    $decoded = Base64::decode($encoded);
} catch(Base64Exception $e) {
    // Handle errors
}

echo $decoded.PHP_EOL;

This will output:

The quick brown fox jumps over the lazy dog

License

Copyright © 2017-2021, Andre Polykanine also known as Menelion Elensúlë, The Magical Kingdom of Oirë.
This software is licensed under an MIT license.