Skip to content

Instantly share code, notes, and snippets.

@NiltonMorais
Last active February 11, 2022 15:14
Show Gist options
  • Save NiltonMorais/8ee46dff5b93dd990e7cecec8ced5665 to your computer and use it in GitHub Desktop.
Save NiltonMorais/8ee46dff5b93dd990e7cecec8ced5665 to your computer and use it in GitHub Desktop.
JWT structure in PHP
<?php
// JWT structure
// header.payload.signature
// $header
$header = [
'alg' => 'HS256', // HMAC - SHA256
'typ' => 'JWT'
];
$headerJson = json_encode($header);
$headerBase64 = base64_encode($headerJson);
echo $headerBase64;
echo "\n\n";
// payload
$payload = [
'name' => 'Nilton',
'email' => 'nilton@email.com',
'exp' => (new \DateTime())->getTimestamp() + 3600
];
$payloadJson = json_encode($payload);
$payloadBase64 = base64_encode($payloadJson);
echo $payloadBase64;
echo "\n\n";
// signature
$key = '76526asdsda125asdasd987asd4';
$signature = hash_hmac('sha256', "$headerBase64.$payloadBase64", $key, true);
$signatureBase64 = base64_encode($signature);
echo "\n\n";
echo $signatureBase64;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment