<commit_before><?php

namespace JWT;

use \DomainException;
use \InvalidArgumentException;
use \UnexpectedValueException;
use \DateTime;

final class JWT
{
	public static $supportedAlg = [
        'HS256' => ['hash_hmac', 'SHA256'],
        'HS512' => ['hash_hmac', 'SHA512'],
        'HS384' => ['hash_hmac', 'SHA384'],
        'RS256' => ['openssl', 'SHA256'],
        'RS384' => ['openssl', 'SHA384'],
        'RS512' => ['openssl', 'SHA512']
    ];

    private static $exp = 0;

    private $headers = [];

    private static $algorithm;

    private $claims = [];

    private $signature = null;

    public function __construct($algorithm = 'HS256')
    {
    	$this->headers = ['typ'=> 'JWT', 'alg' => $algorithm];
        JWT::$algorithm = $algorithm;
    }

    public function sign($key, $algorithm = null)
    {
    	$payload = $this->getPayload();
        $algorithm = empty($algorithm) ? self::getAlgorithm() : $algorithm;

    	if (empty(self::$supportedAlg[$algorithm])) {
            throw new DomainException('Algorithm not supported');
        }

        list($function, $algorithm) = self::$supportedAlg[$algorithm];

        switch($function) {
            case 'hash_hmac':
                $this->signature = hash_hmac($algorithm, $payload, $key, true);

                return $this;
            case 'openssl':
                $signature = '';
                $success = openssl_sign($payload, $signature, $key, $algorithm);
                if (!$success) {
                    throw new DomainException("OpenSSL unable to sign data");
                } else {
                    $this->signature = $signature;

                    return $this;
                }
        }
    }

    public function getToken()
    {
        $payload = [$this->getPayload()];

        if ($this->signature !== null) {
            $payload[] = JWT::URLSafeB64Encode($this->signature);
        }

        return implode('.', $payload);
    }

    public function verifyToken($key, $token = null)
    {
        if(empty($token)){
        	$token = $this;
        } elseif($token instanceof JWT){
            $token = $token->getToken();
        }

        if (empty($key)) {
            throw new InvalidArgumentException('Key may not be empty');
        }

        $token = explode('.', $token);

        if (count($token) != 3) {
            throw new UnexpectedValueException('Wrong number of segments');
        }

        list($headB64, $payloadB64, $cryptoB64) = $token;<commit_msg>Update JWT.php<commit_after><?php

namespace JWT;

use \DomainException;
use \InvalidArgumentException;
use \UnexpectedValueException;
use \DateTime;

final class JWT
{
	public static $supportedAlg = [
        'HS256' => ['hash_hmac', 'SHA256'],
        'HS512' => ['hash_hmac', 'SHA512'],
        'HS384' => ['hash_hmac', 'SHA384'],
        'RS256' => ['openssl', 'SHA256'],
        'RS384' => ['openssl', 'SHA384'],
        'RS512' => ['openssl', 'SHA512']
    ];

    private static $exp = 0;

    private $headers = [];

    private static $algorithm;

    private $claims = [];

    private $signature = null;

    public function __construct($algorithm = 'HS256')
    {
    	$this->headers = ['typ'=> 'JWT', 'alg' => $algorithm];
        JWT::$algorithm = $algorithm;
    }

    public function sign($key, $algorithm = null)
    {
    	$payload = $this->getPayload();
        $algorithm = empty($algorithm) ? self::getAlgorithm() : $algorithm;

    	if (empty(self::$supportedAlg[$algorithm])) {
            throw new DomainException('Algorithm not supported');
        }

        list($function, $algorithm) = self::$supportedAlg[$algorithm];

        switch($function) {
            case 'hash_hmac':
                $this->signature = hash_hmac($algorithm, $payload, $key, true);

                return $this;
            case 'openssl':
                $signature = '';
                $success = openssl_sign($payload, $signature, $key, $algorithm);
                if (!$success) {
                    throw new DomainException("OpenSSL unable to sign data");
                } else {
                    $this->signature = $signature;

                    return $this;
                }
        }
    }

    public function getToken()
    {
        $payload = [$this->getPayload()];

        if ($this->signature !== null) {
            $payload[] = JWT::URLSafeB64Encode($this->signature);
        }

        return implode('.', $payload);
    }

    public function verifyToken($key, $token = null)
    {
        if(empty($token)){
            $token = $this;
        } elseif($token instanceof JWT){
            $token = $token->getToken();
        }

        if (empty($key)) {
            throw new InvalidArgumentException('Key may not be empty');
        }

        $token = explode('.', $token);

        if (count($token) != 3) {
            throw new UnexpectedValueException('Wrong number of segments');
        }

        list($headB64, $payloadB64, $cryptoB64) = $token;
{'fraction_non_alphanumeric': 0.15484775641025642, 'fraction_numerical': 0.019631410256410256, 'mean_word_length': 2.0631901840490796, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1616904', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1376, 'n_words': 328}