<?php

include '../src/JWToken.php';
$payload = array('username' => 'bllohar', 'userId' => 1);
$secret = "^&562!2wzJGH!222";
// Generate token with secret
$token = JWToken::encode($payload, $secret, 'HS512');
echo $token . "<br/><br/><br/>";
try {
    $data = JWToken::decode($token, $secret, 'HS512');
    var_dump($data);
} catch (Exception $e) {
    echo $e->getMessage();
}
/* With algo -	RSA (Public Key/Private Key pair) */
$payload = array('username' => 'bllohar', 'userId' => 1);
$private_key = file_get_contents('keys/private_key.pem');
$public_key = file_get_contents('keys/public_key.pem');
// Generate token with Private key
$token = JWToken::encode($payload, $private_key, 'RS256');
echo $token;
try {
    $data = JWToken::decode($token, $public_key, 'RS256');
    var_dump($data);
} catch (Exception $e) {
    echo $e->getMessage();
}
 public static function decode($jwt, $secret = null, $algo = null)
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new Exception('Wrong number of segments');
     }
     list($headb64, $payloadb64, $cryptob64) = $tks;
     if (null === ($header = json_decode(JWToken::urlsafeB64Decode($headb64)))) {
         throw new Exception('Invalid segment encoding');
     }
     if (null === ($payload = json_decode(JWToken::urlsafeB64Decode($payloadb64)))) {
         throw new Exception('Invalid segment encoding');
     }
     $sig = JWToken::urlsafeB64Decode($cryptob64);
     if (isset($secret)) {
         if (empty($header->alg)) {
             throw new DomainException('Empty algorithm');
         }
         if (!JWToken::verify($sig, "{$headb64}.{$payloadb64}", $secret, $algo)) {
             throw new UnexpectedValueException('Signature verification failed');
         }
     }
     return $payload;
 }