PHP code example of shota / jwt-covertor

1. Go to this page and download the library: Download shota/jwt-covertor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shota / jwt-covertor example snippets


// $sign_seed = random_bytes(SODIUM_CRYPTO_SIGN_SEEDBYTES);
// $sign_pair = sodium_crypto_sign_seed_keypair($sign_seed);

$sign_pair = sodium_crypto_sign_keypair();
$sign_secret = sodium_crypto_sign_secretkey($sign_pair);
$sign_public = sodium_crypto_sign_publickey($sign_pair);

$message = 'Hello';

$signature = sodium_crypto_sign_detached($message, $sign_secret);
$message_valid = sodium_crypto_sign_verify_detached($signature, $message, $sign_public);

use Shota\JWT\JWT;

$key = 'encode-key';
$payload = [
        "iss" => "John Doe",
        "exp" => time() + 100000,
        "sub" => "unit-test-01",
        "aud" => "all",
        "nbf" => time(),
        "iat" => time(),
        "jti" => time(),

        "name" => "John Doe",
        "admin" => true,
];
$jwtStr = JWT::encode($this->payload, $key, 'HS512');
echo sprintf('jwt : %s \n',jwtStr);

$decodePayload = JWT::decode($jwtStr,$key)
print_r($decodePayload);

use Shota\JWT\JWT;

$privateKey = file_get_contents(__DIR__ . '/cert/ecs-private-key.pem');
$publicKey =  file_get_contents(__DIR__ . '/cert/ecs-public-key.pem');

$payload = [
    "iss" => "John Doe",
    "exp" => time() + 100000,
    "sub" => "unit-test-01",
    "aud" => "all",
    "nbf" => time(),
    "iat" => time(),
    "jti" => time(),

    "name" => "John Doe",
    "admin" => true,
];
$jwtStr = JWT::encode($this->payload, $privateKey, 'HS512');
echo sprintf('jwt : %s \n',jwtStr);

$decodePayload = JWT::decode($jwtStr,$publicKey)
print_r($decodePayload);

use Shota\JWT\JWT;

$privateKey = file_get_contents(__DIR__ . '/cert/rsa-private-key.pem');
$publicKey = file_get_contents(__DIR__ . "/cert/rsa-public-key.pem");

$payload = [
    "iss" => "John Doe",
    "exp" => time() + 100000,
    "sub" => "unit-test-01",
    "aud" => "all",
    "nbf" => time(),
    "iat" => time(),
    "jti" => time(),

    "name" => "John Doe",
    "admin" => true,
];

$jwtStr = JWT::encode($this->payload, $privateKey, 'HS512');
echo sprintf('jwt : %s \n',jwtStr);

$decodePayload = JWT::decode($jwtStr,$publicKey)
print_r($decodePayload);

use Shota\JWT\JWT;

$payload = [
    "iss" => "John Doe",
    "exp" => time() + 100000,
    "sub" => "unit-test-01",
    "aud" => "all",
    "nbf" => time(),
    "iat" => time(),
    "jti" => time(),

    "name" => "John Doe",
    "admin" => true,
];

$signPair = sodium_crypto_sign_keypair();
$secret = sodium_crypto_sign_secretkey($signPair);
$publicKey = sodium_crypto_sign_publickey($signPair);

$jwtStr = JWT::encode($payload, $secret, 'EdDSA');
echo sprintf('jwt : %s \n',jwtStr);

$decodePayload = JWT::decode(jwtStr, publicKey);
print_r($decodePayload);