Skip to content
This repository was archived by the owner on Mar 8, 2021. It is now read-only.
/ Base64 Public archive

URL-safe Base64 handling in PHP 7.

License

Notifications You must be signed in to change notification settings

Oire/Base64

Repository files navigation

Oirë Base64, URL-safe Base64 Handling

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.

About

URL-safe Base64 handling in PHP 7.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages