3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class JWT { /** * Decodes a JWT string into a PHP object. * * @param string $jwt The JWT * @param string|null $key The secret key * @param bool $verify Don't skip verification process * * @throws UnexpectedValueException Provided JWT was invalid * @throws DomainException Algorithm was not provided * * @return object The JWT's payload as a PHP object * * @uses jsonDecode * @uses urlsafeB64Decode */ public static function decode($jwt, $key = null, $verify = true) { $tks = explode('.', $jwt); if (count($tks) != 3) { throw new \UnexpectedValueException('Wrong number of segments'); } list($headb64, $bodyb64, $cryptob64) = $tks; if (null === ($header = self::jsonDecode(self::urlsafeB64Decode($headb64)))) { throw new \UnexpectedValueException('Invalid segment encoding'); } if (null === $payload = self::jsonDecode(self::urlsafeB64Decode($bodyb64))) { throw new \UnexpectedValueException('Invalid segment encoding'); } $sig = self::urlsafeB64Decode($cryptob64); if ($verify) { if (empty($header->alg)) { throw new \DomainException('Empty algorithm'); } if ($sig != self::sign("$headb64.$bodyb64", $key, $header->alg)) { throw new \UnexpectedValueException('Signature verification failed'); } } return $payload; } /** * Converts and signs a PHP object or array into a JWT string. * * @param object|array $payload PHP object or array * @param string $key The secret key * @param string $algo The signing algorithm. Supported * algorithms are 'HS256', 'HS384' and 'HS512' * * @return string A signed JWT * * @uses jsonEncode * @uses urlsafeB64Encode */ public static function encode($payload, $key, $algo = 'HS256') { $header = ['typ' => 'JWT', 'alg' => $algo]; $segments = []; $segments[] = self::urlsafeB64Encode(self::jsonEncode($header)); $segments[] = self::urlsafeB64Encode(self::jsonEncode($payload)); $signing_input = implode('.', $segments); $signature = self::sign($signing_input, $key, $algo); $segments[] = self::urlsafeB64Encode($signature); return implode('.', $segments); } /** * Sign a string with a given key and algorithm. * * @param string $msg The message to sign * @param string $key The secret key * @param string $method The signing algorithm. Supported * algorithms are 'HS256', 'HS384' and 'HS512' * * @throws DomainException Unsupported algorithm was specified * @return string An encrypted message */ private static function sign($msg, $key, $method = 'HS256') { $methods = [ 'HS256' => 'sha256', 'HS384' => 'sha384', 'HS512' => 'sha512', ]; if (empty($methods[$method])) { throw new \DomainException('Algorithm not supported'); } return hash_hmac($methods[$method], $msg, $key, true); } /** * Decode a JSON string into a PHP object. * * @param string $input JSON string * @throws DomainException Provided string was invalid JSON * @return object Object representation of JSON string */ private static function jsonDecode($input) { $obj = json_decode($input); if (function_exists('json_last_error') && $errno = json_last_error()) { self::_handleJsonError($errno); } elseif ($obj === null && $input !== 'null') { throw new \DomainException('Null result with non-null input'); } return $obj; } /** * Encode a PHP object into a JSON string. * * @param object|array $input A PHP object or array * @throws DomainException Provided object could not be encoded to valid JSON * @return string JSON representation of the PHP object or array */ private static function jsonEncode($input) { $json = json_encode($input); if (function_exists('json_last_error') && $errno = json_last_error()) { self::_handleJsonError($errno); } elseif ($json === 'null' && $input !== null) { throw new \DomainException('Null result with non-null input'); } return $json; } /** * Decode a string with URL-safe Base64. * * @param string $input A Base64 encoded string * @return string A decoded string */ private static function urlsafeB64Decode($input) { $remainder = strlen($input) % 4; if ($remainder) { $padlen = 4 - $remainder; $input .= str_repeat('=', $padlen); } return base64_decode(strtr($input, '-_', '+/')); } /** * Encode a string with URL-safe Base64. * * @param string $input The string you want encoded * @return string The base64 encode of what you passed in */ private static function urlsafeB64Encode($input) { return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); } /** * Helper method to create a JSON error. * * @param int $errno An error number from json_last_error() * @return void */ private static function _handleJsonError($errno) { $messages = [ JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', ]; throw new \DomainException( isset($messages[$errno]) ? $messages[$errno] : 'Unknown JSON error: '.$errno ); } } $secret = 'SERVERSIDE_SECRET'; try { $token = JWT::encode(['user_id' => 1], $secret); echo $token.PHP_EOL; // verify if (JWT::decode($token, $secret, true)) { echo 'Token valid'.PHP_EOL; } else { echo 'Token invalid'; } // tampered try { JWT::decode($token.'X', $secret, true); } catch (\Exception $e) { echo $e->getMessage().PHP_EOL; } // decode print_r(JWT::decode($token, $secret)); } catch (\Exception $e) { echo $e->getMessage(); }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.00.0110.01220.30
8.4.150.0030.00014.05
8.4.140.0090.01217.98
8.4.130.0120.01019.83
8.4.120.0130.00720.53
8.4.110.0120.01022.63
8.4.100.0070.00218.16
8.4.90.0120.00817.94
8.4.80.0110.01017.94
8.4.70.0130.00818.07
8.4.60.0120.00918.16
8.4.50.0110.01017.99
8.4.40.0060.00320.46
8.4.30.0070.01320.29
8.4.20.0100.00717.88
8.4.10.0000.00919.73
8.3.280.0140.00918.51
8.3.270.0100.01016.75
8.3.260.0130.00616.84
8.3.250.0050.00318.90
8.3.240.0100.01117.13
8.3.230.0060.01316.70
8.3.220.0060.00217.32
8.3.210.0100.00816.82
8.3.200.0100.00917.00
8.3.190.0090.01016.41
8.3.180.0130.00717.02
8.3.170.0090.00916.66
8.3.160.0110.00718.55
8.3.150.0040.00417.50
8.3.140.0000.00820.58
8.3.130.0080.00318.47
8.3.120.0050.00320.74
8.3.110.0030.00620.94
8.3.100.0120.00624.06
8.3.90.0060.00326.77
8.3.80.0060.00319.36
8.3.70.0120.00316.88
8.3.60.0110.01118.68
8.3.50.0070.01023.81
8.3.40.0150.00321.93
8.3.30.0110.01118.83
8.3.20.0030.00624.18
8.3.10.0050.00324.66
8.3.00.0030.00626.16
8.2.290.0060.00420.45
8.2.280.0060.00218.46
8.2.270.0130.00716.84
8.2.260.0100.00617.30
8.2.250.0060.00316.69
8.2.240.0060.00617.16
8.2.230.0060.00322.58
8.2.220.0000.01037.54
8.2.210.0000.00826.77
8.2.200.0000.00918.88
8.2.190.0090.01316.92
8.2.180.0180.00425.92
8.2.170.0160.00618.92
8.2.160.0070.00722.96
8.2.150.0040.00425.66
8.2.140.0030.00524.66
8.2.130.0040.00426.16
8.2.120.0040.00419.95
8.2.110.0030.00619.39
8.2.100.0080.00417.84
8.2.90.0000.00920.42
8.2.80.0000.00820.48
8.2.70.0030.00617.63
8.2.60.0040.00417.75
8.2.50.0030.00518.05
8.2.40.0050.00318.16
8.2.30.0070.00018.13
8.2.20.0080.00018.27
8.2.10.0000.00819.40
8.2.00.0040.00419.42
8.1.330.0100.00916.97
8.1.320.0110.00916.23
8.1.310.0080.00018.73
8.1.300.0060.00316.23
8.1.290.0060.00318.88
8.1.280.0030.01225.92
8.1.270.0030.00523.99
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0030.00623.87
8.1.230.0080.00421.03
8.1.220.0060.00317.74
8.1.210.0080.00018.91
8.1.200.0030.00617.48
8.1.190.0050.00517.35
8.1.180.0060.00318.10
8.1.170.0040.00418.59
8.1.160.0030.00618.82
8.1.150.0030.00718.86
8.1.140.0030.00520.59
8.1.130.0070.00020.11
8.1.120.0040.00417.53
8.1.110.0040.00417.39
8.1.100.0070.00017.47
8.1.90.0100.00017.40
8.1.80.0030.00617.41
8.1.70.0080.00017.54
8.1.60.0080.00017.63
8.1.50.0000.00817.46
8.1.40.0040.00417.45
8.1.30.0060.00317.72
8.1.20.0000.00817.74
8.1.10.0050.00317.61
8.1.00.0030.00617.57
8.0.300.0050.00320.20
8.0.290.0090.00316.88
8.0.280.0040.00418.52
8.0.270.0000.00718.11
8.0.260.0000.00618.63
8.0.250.0030.00317.09
8.0.240.0060.00317.05
8.0.230.0000.00717.05
8.0.220.0070.00016.91
8.0.210.0000.00816.90
8.0.200.0000.00716.98
8.0.190.0020.00517.00
8.0.180.0040.00417.02
8.0.170.0030.00617.03
8.0.160.0040.00417.10
8.0.150.0000.00816.96
8.0.140.0070.00016.95
8.0.130.0060.00013.48
8.0.120.0000.00816.90
8.0.110.0000.00716.98
8.0.100.0040.00417.04
8.0.90.0000.00716.87
8.0.80.0100.00516.93
8.0.70.0030.00616.88
8.0.60.0040.00417.07
8.0.50.0000.00716.96
8.0.30.0120.01216.86
8.0.20.0170.00517.25
8.0.10.0000.00917.09
8.0.00.0130.00716.79
7.4.330.0060.00015.55
7.4.320.0000.00616.66
7.4.300.0000.00716.48
7.4.290.0000.00816.50
7.4.280.0060.00316.64
7.4.270.0030.00316.52
7.4.260.0030.00313.29
7.4.250.0040.00416.59
7.4.240.0020.00616.44
7.4.230.0000.00716.54
7.4.220.0050.00216.64
7.4.210.0070.00916.67
7.4.200.0040.00416.49
7.4.130.0140.00816.57
7.4.120.0140.01016.37
7.4.110.0100.01116.55
7.4.100.0100.01116.41
7.4.90.0140.01016.48
7.4.80.0150.00616.38
7.4.70.0140.01016.64
7.4.60.0070.01116.53
7.4.50.0070.01116.27
7.4.40.0120.00916.22
7.4.30.0160.00816.30
7.4.20.0130.00616.45
7.4.10.0100.01216.53
7.4.00.0100.01416.55
7.3.330.0030.00316.41
7.3.320.0030.00313.46
7.3.310.0000.00716.36
7.3.300.0030.00316.48
7.3.290.0040.00416.47
7.3.260.0070.01316.80
7.3.240.0090.01516.29
7.3.230.0120.00916.43
7.3.220.0090.00916.29
7.3.210.0070.01116.38
7.3.200.0100.01016.36
7.3.190.0000.01816.48
7.3.180.0030.01716.46
7.3.170.0170.00616.43
7.3.160.0120.00616.47
7.3.150.0190.00016.36
7.3.140.0090.01216.34
7.3.130.0130.00616.32
7.3.120.0090.01216.53
7.3.110.0040.01416.37
7.3.100.0100.01016.37
7.3.90.0100.00916.36
7.3.80.0150.00916.41
7.3.70.0110.00616.57
7.3.60.0150.00616.55
7.3.50.0120.00916.38
7.3.40.0060.01216.40
7.3.30.0090.00916.45
7.3.20.0260.01016.39
7.3.10.0090.00916.41
7.3.00.0110.00616.47
7.2.340.0120.00616.57
7.2.330.0150.01116.63
7.2.320.0030.01716.69
7.2.310.0090.00916.75
7.2.300.0130.01016.57
7.2.290.0040.01316.47
7.2.280.0100.01116.63
7.2.270.0130.00616.79
7.2.260.0140.00716.48
7.2.250.0090.01316.70
7.2.240.0060.01216.70
7.2.230.0170.00716.71
7.2.220.0130.00716.48
7.2.210.0070.01116.72
7.2.200.0130.01016.64
7.2.190.0070.01016.78
7.2.180.0060.01616.68
7.2.170.0120.00816.50
7.2.160.0170.00716.59
7.2.150.0210.00016.74
7.2.140.0150.00616.77
7.2.130.0100.01016.66
7.2.120.0110.00716.77
7.2.110.0130.01316.65
7.2.100.0100.01316.82
7.2.90.0130.01316.75
7.2.80.0130.01316.66
7.2.70.0120.01516.86
7.2.60.0090.01516.71
7.2.50.0190.00016.63
7.2.40.0150.00616.70
7.2.30.0130.01016.85
7.2.20.0120.01216.83
7.2.10.0200.00716.65
7.2.00.0090.01116.68

preferences:
175.57 ms | 403 KiB | 5 Q