Advertisement
Guest User

Untitled

a guest
May 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. $jwt="/root/transcoder/jwt_key.json";
  4. $token="eyJhbGciOiJIUzI1NiJ9.eyJjaGFsbGVuZ2UiOiI2ZmZjMjkwZDg0YjJhNzk2YWZiYmMxMTljMTZkYWViYjU3NTIxOTc5MWU3YWZlODMwMjI2MGIwYTVhMDNiZGQ0ZTU0NTRiNzg4ZDM2Mjk2YjQ0OTFiYjcxN2I0YjU4MzMiLCJleHAiOjE1NjEzNjU0ODcsImVtYWlsIjoiY3Jpc3RpQG5vdmFuZXQucm8ifQ.pGS4J_9sPafuJ78zAAkzh8VXlxy-XK2zv7Kvxs7Oo7g";
  5.  
  6. $key=get_key($jwt);
  7.  
  8. print("\ntoken: $token \nkey: $key\n");
  9.  
  10. if(validate($token, $key)) print("\nvalidate"); else print("\ninvalidate");
  11. print("\n");
  12.  
  13.  
  14. /////////////////////////////////////////////////////////
  15.  
  16. function get_key($jwt="/root/transcoder/jwt_key.json") {
  17.         $j=file_get_contents($jwt);
  18.         $json=json_decode($j, true);
  19.         return $json["k"];
  20. }
  21.  
  22. function urlsafeB64Decode($input) {
  23.         $remainder = strlen($input) % 4;
  24.         if ($remainder) {
  25.                 $padlen = 4 - $remainder;
  26.                 $input .= str_repeat('=', $padlen);
  27.         }
  28.         return base64_decode(strtr($input, '-_', '+/'));
  29. }
  30.  
  31. function jsonDecode($input) {
  32.         return json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
  33. }
  34.  
  35. function validate($token, $key){
  36.         $tks = explode('.', $token);
  37.         list($headb64, $bodyb64, $cryptob64) = $tks;
  38.  
  39.         $header = jsonDecode(urlsafeB64Decode($headb64));
  40.         $payload = jsonDecode(jsonDecode($bodyb64));
  41.         $sig = urlsafeB64Decode($cryptob64);
  42.  
  43.         $hash = hash_hmac("sha256", "$headb64.$bodyb64", urlsafeB64Decode($key), true);
  44.         return hash_equals($sig, $hash);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement