Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active July 21, 2017 21:03
Show Gist options
  • Save tuupola/c4ce6ed3b6fa1cdf62783e976d96cd24 to your computer and use it in GitHub Desktop.
Save tuupola/c4ce6ed3b6fa1cdf62783e976d96cd24 to your computer and use it in GitHub Desktop.
Compare generated (API) token length between Branca and JWT.
<?php
/* https://github.com/tuupola/branca */
require __DIR__ . "/vendor/autoload.php";
use Branca\Branca;
use Branca\Protobuf\Payload as Protobuf;
use Firebase\JWT\JWT;
use MessagePack\Packer;
use MessagePack\Unpacker;
$key = "supersecretkeyyoushouldnotcommit";
/* Unlike JWT, Branca has timestamp in header, not in payload. */
$timestamp = 1356999524;
$payload = [
"iss" => "https://example.org",
"sub" => "[email protected]",
"nbf" => 1357000000
];
$json = json_encode($payload);
$msgpack = (new Packer)->pack($payload);
$protobuf = new Protobuf;
$protobuf->setIss("https://example.org");
$protobuf->setSub("[email protected]");
$protobuf->setNbf(1357000000);
$branca = new Branca($key);
print "** Branca and JSON\n";
$token = $branca->encode($json, $timestamp);
var_dump($token);
print "\n";
print "** Branca and MessagePack\n";
$token = $branca->encode($msgpack, $timestamp);
var_dump($token);
print "\n";
print "** Branca and Protocol Buffers\n";
$token = $branca->encode($protobuf->serializeToString(), $timestamp);
var_dump($token);
print "\n";
/* JWT has timestamp in iat claim. */
$payload["iat"] = $timestamp;
print "** JWT\n";
$jwt = JWT::encode($payload, $key);
var_dump($jwt);
print "\n";
** Branca and JSON
/Users/tuupola/Code/php/branca-test/compare.php:37:
string(151) "5K0UhjLYfxeKOteNITvgDRIksLdssYQq3QY631RG8lCtfJEvToIczksrCxbeon9cuKBZZIJduKlY7Tyh48u76vmtm9UBLLJcMXfgKXrpzO8mJpmGhXONkTWPcdvMdI14oG5LTDYYl30CIuVCgh0Ioas"
** Branca and MessagePack
/Users/tuupola/Code/php/branca-test/compare.php:42:
string(127) "2U1rjxk6WT2xOX5MzaI7ZInfxwuEjtOFUISh8Gu4EiqvvpUTqT32bNyMREHZtpDvK5tGNxFHJsfcbREXrRGOSwfUFk0szovOfuqrSkH6dLPT49kZOTyDIrfgNFHy95O"
** Branca and Protocol Buffers
/Users/tuupola/Code/php/branca-test/compare.php:47:
string(113) "PU7L2pPyN5iBYdUlGSXAL903ywxL06R7Z5r5rmGAt90Vccv57kt7chI3xrDquGYnSGb3Luhj9hedEGNPbKS3tjDRyk8ivfbkLJvc9qw22pyAsDQI4"
** JWT
/Users/tuupola/Code/php/branca-test/compare.php:56:
string(209) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvZXhhbXBsZS5vcmciLCJzdWIiOiJ0dXVwb2xhQGFwcGVsc2lpbmkubmV0IiwibmJmIjoxMzU3MDAwMDAwLCJpYXQiOjEzNTY5OTk1MjR9.TG4zI3uIdalmBXRojIgFIEVA3QAcOVCLFvdwEN_-q7c"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment