Skip to content

Instantly share code, notes, and snippets.

@ArthurHoaro
Created January 17, 2017 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArthurHoaro/0d1bdbbaeb42c52470c745f8ac6e44f1 to your computer and use it in GitHub Desktop.
Save ArthurHoaro/0d1bdbbaeb42c52470c745f8ac6e44f1 to your computer and use it in GitHub Desktop.
Example PHP JWT token generation using SHA512 encryption
<?php
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function generateJwtToken($secret) {
$header = base64_encode('{
"typ": "JWT",
"alg": "HS512"
}');
$payload = base64_encode('{
"iat": '. time() .'
}');
$signature = base64url_encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
return $header .'.'. $payload .'.'. $signature;
}
$token = generateJwtToken('secretToken');
echo $token;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment