Skip to content

Instantly share code, notes, and snippets.

@johannesl
Created May 29, 2018 20:45
Show Gist options
  • Save johannesl/a61de9c639832397aed7f0fecb4e64c8 to your computer and use it in GitHub Desktop.
Save johannesl/a61de9c639832397aed7f0fecb4e64c8 to your computer and use it in GitHub Desktop.
JSON Web Token without third-party dependencies
<?php
function jwt_hs256_encode( $payload, $secret ) {
$headers = array( 'alg' => 'HS256', 'typ' => 'JWT' );
$parts[] = base64url_encode( json_encode( $headers, JSON_FORCE_OBJECT ) );
$parts[] = base64url_encode( json_encode( $payload, JSON_FORCE_OBJECT ) );
$signature = hash_hmac( 'SHA256', $parts[0] .'.'. $parts[1], $secret, true );
$parts[] = base64url_encode( $signature );
return implode( '.', $parts );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment