Generate a CSRF token

@param  string $key Key for this token
@return string
326455
public function generate($key)
	{
		$key = $result = preg_replace('/[^a-zA-Z0-9]+/', '', $key);

		$extra = sha1($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
		// time() is used for token expiration
		$token = base64_encode(time() . $extra . $this->randomString(32));
		$this->session->set($this->session_prefix . $key, $token);

		return $token;
	}
574970
public function loadToken($key)
    {
        $token = $this->db
            ->select('ucms_user_token', 'ut')
            ->fields('ut')
            ->condition('token', $key)
            ->range(0, 1)
            ->execute()
            ->fetchObject('MakinaCorpus\\Ucms\\User\\Token');

        if (!$token) {
            return null;
        }

        $token->expiration_date = new \DateTime($token->expiration_date);
        return $token;
    }
469543
protected function prettyHeaderKey(string $key) : string
    {
        return strtr(ucwords(strtr(strtr(strtolower($key), "_", " "), "-", " ")), " ", "-");
    }
105583
public function getCsrfToken() {
		if (!isset($_SESSION[$this->csrfTokenSessionKey])) {
			$this->generateCsrfToken();
		}
		return $_SESSION[$this->csrfTokenSessionKey];
	}
864183
public function generateToken()
    {
        $token   = md5(__CLASS__.uniqid());

        $sesCont = new SessionContainer(self::SESSION_KEY);
        $sesCont->$token = time();
        $sesCont->setExpirationSeconds(30, $token);

        return $token;
    }
528091
public function cookie(string $key): string
    {
        if (array_key_exists($key, $this->cookies)) {
            return $this->decryptor->decrypt($this->cookies[$key]);
        }

        return '';
    }
1133661
protected function tokensMatch($request)
	{
		$token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');

		if ( ! $token && $header = $request->header('X-XSRF-TOKEN'))
		{
			$token = $this->encrypter->decrypt($header);
		}

		return StringUtils::equals($request->session()->token(), $token);
	}
1182961
public static function generateToken()
    {
        // Generate the token based on the current time, plus a random string.
        $token = time() . '.';

        // Start with an empty string.
        $length       = 32;
        $characterset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';

        // For the desired length, keep adding random characters from the character set.
        for ($i = 0; $i < $length; $i++) {
            $token .= $characterset[mt_rand(0, strlen($characterset) - 1)];
        }

        $sessions = [];

        if (isset($_SESSION['csrftokens'])) {
            $sessions = array_slice($_SESSION['csrftokens'], -50, 50);
        }

        // Add the token to the "token-basket", a place in which we store all the currently valid CSRF tokens.
        $sessions[] = $token;

        $_SESSION['csrftokens'] = $sessions;

        return $token;
    }
431460
protected function getCsrfToken(): ?string
    {
        if (is_null($this->csrfToken)) {
            $this->csrfToken = $this->app->make('session')->token();
        }

        return $this->csrfToken;
    }
551524
public static function generateSign($data, $key = null): string
    {
        if (is_null($key)) {
            throw new InvalidArgumentException('Missing Wechat Config -- [key]');
        }

        ksort($data);

        $string = md5(self::getSignContent($data).'&key='.$key);

        return strtoupper($string);
    }
1082956
public function normalizeKey($key)
    {
        $key = strtolower($key);
        $key = str_replace(array('-', '_'), ' ', $key);
        $key = preg_replace('#^http #', '', $key);
        $key = ucwords($key);
        $key = str_replace(' ', '-', $key);

        return $key;
    }
1236260
public function get(string $key): string
    {
        $key = $this->getKey($key);
        $value = $this->headers[$key] ?? "";
        return is_array($value) ? implode(", ", $value) : $value;
    }
307389
public function setAuthKey($key)
    {
        $authKeyAttribute = $this->authKeyAttribute;
        return (is_string($authKeyAttribute) && !empty($authKeyAttribute)) ? $this->$authKeyAttribute = $key : null;
    }
162326
public static function getPKCS1($key)
	{
		$prefix = '-----BEGIN RSA PUBLIC KEY-----';
		$postfix = '-----END RSA PUBLIC KEY-----';
		
		$key = str_replace($prefix, '', $key);
		$key = str_replace($postfix, '', $key);
		$key = str_replace("\n", '', $key);
		
		$lines = str_split($key, 65);
		$body = implode("\n", $lines);
		$result = $prefix . "\n";
		$result .= $body . "\n";
		$result .= $postfix;
		
		return $result;
	}
654916
public function generateCode($key)
	{
		if($this->extends === NULL && $this->output != '')
		{
			$this->buffer .= "\t\t\$out->write(" . var_export($this->output, true) .  ");\n";
			$this->output = '';
		}
		
		$buffer = sprintf("<?php\n\nnamespace %s {\n\n", __NAMESPACE__);
		$buffer .= 'use KoolKode\View\TagBuilder;' . "\n";
		
		$buffer .= "\n";
		$buffer .= sprintf('final class CompiledTemplate_%s extends CompiledTemplate {' . "\n\n", $key);
		
		for($count = count($this->fields), $step = 8, $i = 0; $i < $count; $i += $step)
		{
			$buffer .= "\tprivate \$" . $this->fields[$i];
			
			for($max = min($count, $i + $step), $j = $i + 1; $j < $max; $j++)
			{
				$buffer .= ', $' . $this->fields[$j];
			}
			
			$buffer .= ";\n";
		}
		
		$buffer .= "\n\tpublic function getResource() {\n";
		$buffer .= "\t\treturn " . var_export($this->resource, true) . ";\n";
		$buffer .= "\t}\n";
		
		if($this->extends === NULL)
		{
			$buffer .= "\n\tprotected function renderMain(OutputBuffer \$out) {\n\n";
			$buffer .= "\t\t\$pout = \$this->context->set('@out', \$out);\n";
			$buffer .= "\t\t\$pthis = \$this->context->set('@this', \$this);\n";
			$buffer .= "\t\ttry {\n";
			$buffer .= "\n" . $this->buffer . "\n";
			$buffer .= "\t\t} finally {\n";
			$buffer .= "\t\t\t\$this->context->set('@out', \$pout);\n";
			$buffer .= "\t\t\t\$this->context->set('@this', \$pthis);\n";
			$buffer .= "\t\t}\n";
			$buffer .= "\t}\n";
		}
		else
		{
			$compiler = new self($this->renderer);
				
			foreach($this->extends as $i => $node)
			{
				if($i != 0)
				{
					$compiler->write(' . ');
				}
		
				$node->compile($compiler, NodeInterface::FLAG_RAW);
			}
			
			$buffer .= "\n\tprotected function getExtended() {\n";
			$buffer .= "\t\treturn " . $compiler->getBuffer() . ";\n";
			$buffer .= "\t}\n";
		}
		
		foreach($this->blocks as $block)
		{
			$buffer .= "\n" . $block;
		}
		
		$buffer .= "}}\n ?>";
		
		return $buffer;
	}
164571
public function getCsrfProtectionToken()
    {
        $sessionDataContainer = $this->objectManager->get(SessionDataContainer::class);
        $csrfProtectionTokens = $sessionDataContainer->getCsrfProtectionTokens();
        if ($this->csrfProtectionStrategy === self::CSRF_ONE_PER_SESSION && count($csrfProtectionTokens) === 1) {
            reset($csrfProtectionTokens);
            return key($csrfProtectionTokens);
        }

        if ($this->csrfProtectionStrategy === self::CSRF_ONE_PER_REQUEST) {
            if (empty($this->requestCsrfToken)) {
                $this->requestCsrfToken = Algorithms::generateRandomToken(16);
                $csrfProtectionTokens[$this->requestCsrfToken] = true;
                $sessionDataContainer->setCsrfProtectionTokens($csrfProtectionTokens);
            }

            return $this->requestCsrfToken;
        }

        $newToken = Algorithms::generateRandomToken(16);
        $csrfProtectionTokens[$newToken] = true;
        $sessionDataContainer->setCsrfProtectionTokens($csrfProtectionTokens);

        return $newToken;
    }
1091458
protected function generateCsrfToken()
    {
        $token = $this->app->getSecurity()->generateRandomString();
        if ($this->enableCsrfCookie) {
            $cookie = $this->createCsrfCookie($token);
            $this->app->getResponse()->getCookies()->add($cookie);
        } else {
            $this->app->getSession()->set($this->csrfParam, $token);
        }

        return $token;
    }