Converts a selector and token pair to base64 forth and back.

1.1.0 2017-08-18 07:51 UTC


Converts a selector and token pair to base64 forth and back. It is tested with Anthony Ferrara's ircmaxell/RandomLib and Paragonie's paragonie/random_compat

Build Status Code Coverage Scrutinizer Code Quality Build Status


composer require germania-kg/base64-coder



use Germania\Base64Coder\Base64Coder;
use Germania\Base64Coder\Exceptions\EncodingException;
use Germania\Base64Coder\Exceptions\DecodingException;

// Optional:
$logger    = new Monolog;
$separator = "::" // Default;

// Setup
$coder = new Base64Coder( $separator, $logger );
$coder = new Base64Coder( $separator );
$coder = new Base64Coder;

Encoding selector and token

$selector = "user_john";
$token    = "somerandomvalue";

try {
	$encoded = $coder->encode( $selector, $token);
	// Result is something like
	// "RGllcyBpc3ftvZ::GllcmVuZGVyIFN0cmluZw=="
catch (EncodingException $e) {
	// PHP's base64_encode had returned FALSE

Decoding encoded selector and token pair

$encoded_value = 'RGllcyBpc3ftvZ::GllcmVuZGVyIFN0cmluZw==';

try {
	$decoded = $cookie_coder->decode( $encoded_value );

	// Result object:
	echo $decoded->selector; // "user_john"
	echo $decoded->token;    // "somerandomvalue"
catch (DecodingException $e) {
	// PHP's base64_decode had returned FALSE


use Germania\Base64Coder\Exceptions\CoderExceptionInterface;

class EncodingException implements CoderExceptionInterface {}
class DecodingException implements CoderExceptionInterface {}

Pimple Service Provider

use Germania\Base64Coder\Providers\PimpleServiceProvider;
use Psr\Log\LoggerInterface;

// have your Pimple DIC ready, and optionally a PSR3 Logger:
$sp = new PimpleServiceProvider;
$custom_separator = "::";
$sp = new PimpleServiceProvider($separator, $psr3_logger);

$sp->register( $dic );

// Grab your services;
// See also above examaples.
$encoder = $dic['Cookie.Encryptor'];
$encoded = $encoder("selector", "token");

$decoder = $dic['Cookie.Decryptor'];
$decoded = $decoder( $encoded );
echo $decoded->selector;
echo $decoded->token;


See issues list.


$ git clone base64-coder
$ cd base64-coder
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit like this:

$ vendor/bin/phpunit