serialize($sig), (new PemPublicKeySerializer(new DerPublicKeySerializer()))->serialize($signingKey), OPENSSL_ALGO_SHA384 ); switch($v){ case 0: return false; case 1: return true; case -1: throw new JwtException("Error verifying JWT signature: " . openssl_error_string()); default: throw new AssumptionFailedError("openssl_verify() should only return -1, 0 or 1"); } } /** * @phpstan-param array $header * @phpstan-param array $claims */ public static function create(array $header, array $claims, PrivateKeyInterface $signingKey) : string{ $jwtBody = JwtUtils::b64UrlEncode(json_encode($header)) . "." . JwtUtils::b64UrlEncode(json_encode($claims)); openssl_sign( $jwtBody, $sig, (new PemPrivateKeySerializer(new DerPrivateKeySerializer()))->serialize($signingKey), OPENSSL_ALGO_SHA384 ); $decodedSig = (new DerSignatureSerializer())->parse($sig); $jwtSig = JwtUtils::b64UrlEncode( hex2bin(str_pad(gmp_strval($decodedSig->getR(), 16), 96, "0", STR_PAD_LEFT)) . hex2bin(str_pad(gmp_strval($decodedSig->getS(), 16), 96, "0", STR_PAD_LEFT)) ); return "$jwtBody.$jwtSig"; } public static function b64UrlEncode(string $str) : string{ return rtrim(strtr(base64_encode($str), '+/', '-_'), '='); } public static function b64UrlDecode(string $str) : string{ if(($len = strlen($str) % 4) !== 0){ $str .= str_repeat('=', 4 - $len); } $decoded = base64_decode(strtr($str, '-_', '+/'), true); if($decoded === false){ throw new JwtException("Malformed base64url encoded payload could not be decoded"); } return $decoded; } }