Generates CSRF token

@param int $length Number of bytes to use to generate token
@return string
@throws \Exception
435647
public function generateCsrfToken(int $length = 32): string
    {
        $token = bin2hex(random_bytes($length));

        (new Session())->set('csrf_token', $token);

        return $token;
    }
813575
static public function generateString($length) {
        $token = '';
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $maxIndex = strlen($chars) - 1;
        
        for ($i = 0; $i < $length; $i++) {
            $token .= $chars[mt_rand(0, $maxIndex)];
        }
         
        return $token;
    }
686838
private static function generateSalt (int $length) : string
	{
		// Not 100% unique, not 100% random, but good enough for a salt
		// MD5 returns 32 characters
		$uniqueRandomString = md5(uniqid(mt_rand(),true));

		// Valid Characters for a salt are [a-AZ-Z0-9./]
		$base64String = base64_encode($uniqueRandomString);

		// But no '+' which is valid in base64 encoding
		$modifiedBase64String = str_replace('+', '.', $base64String);

		// Truncate String to the correct length
		return substr($modifiedBase64String, 0, $length);
	}
574502
public function generateToken($length = 10, $chars = '0123456789abcdefghijklmnopqrstuvwxyz') {
		$token = '';
		$i = 0;
		while ($i < $length) {
			$char = substr($chars, mt_rand(0, strlen($chars) - 1), 1);
			if (!stristr($token, $char)) {
				$token .= $char;
				$i++;
			}
		}

		return $token;
	}
267356
protected static function generateRandomString($length)
    {
        $characters = static::CHARACTERS;

        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }

        return $randomString;
    }
862458
public function generateRandomString($length)
    {
        do {
            $bytes = base64_encode($this->random->nextBytes($length));
            $string = substr(strtr($bytes, array('+' => '', '/' => '', '=' => '')), 0, $length);
        } while (strlen($string) < $length);
        return $string;
    }
595610
protected function GetCSRFToken()
	{
		$token = $this->Context->Get('X-CSRFToken');
	
		if (!$token)
		{
			$token = md5(rand(1111111111,9999999999).microtime());
			$this->Context->Set('X-CSRFToken',$token);
		}
	
		return $token;
	}
1097620
public function generatePassword(int $length = 8)
    {
        if ($length <= 0)
            throw new DomainException("Cannot generate zero-length passwords");

        if (empty($this->characters))
            throw new UnderflowException("First add characters used to generate the password");

        $chars = implode('', array_keys($this->characters));
        $max_val = mb_strlen($chars) - 1;
        $pwd = "";
        for ($i = 0; $i < $length; ++$i)
        {
            $pos = random_int(0, $max_val);
            $pwd .= mb_substr($chars, $pos, 1);
        }
        return $pwd;
    }
1045108
protected function generateToken($type = 'ACCESS', $length = 32, $strong = true)
    {
        if ( ! empty($this->consumer->secret)) {
            return [
                'oauth_token'        => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong),
                    $this->consumer->secret),
                'oauth_token_secret' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong),
                    $this->consumer->secret),
            ];
            switch ($this->oauth->signature_method) {
                default:
                case OAUTH_SIG_METHOD_HMACSHA1:
                case OAUTH_SIG_METHOD_RSASHA1:

                    $token = [
                        'key'    => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong),
                            $this->consumer->secret),
                        'secret' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong),
                            $this->consumer->secret),
                    ];
                    break;

                case OAUTH_SIG_METHOD_HMACSHA256:

                    $token = [
                        'key'    => hash_hmac('sha256', \OAuthProvider::generateToken($length, $strong),
                            $this->consumer->secret),
                        'secret' => hash_hmac('sha256', \OAuthProvider::generateToken($length, $strong),
                            $this->consumer->secret),
                    ];
                    break;
            }

            $nonce = (empty($this->oauth->nonce) ? Oauth::generateNonce() : $this->oauth->nonce);
            $callback = (empty($this->oauth->callback) ? null : $this->oauth->callback);

            if ($this->model->insertToken([
                'id_consumer' => $this->consumer->id,
                'key'         => $token[ 'key' ],
                'secret'      => $token[ 'secret' ],
                'type'        => $type,
                'callback'    => $callback,
            ])) {
                $token[ 'id' ] = $this->model->db->getLastInsertId();

                if ($this->model->insertTokenNonce([
                    'id_consumer_token' => $token[ 'id' ],
                    'nonce'             => $nonce,
                    'timestamp'         => date('Y-m-d H:m:s'),
                    'expires'           => time() + 3600,
                ])) {
                    return $token;
                }
            }
        }

        return false;
    }
752473
protected function generateValue($length) {
	
		if(!(int) $length) {
			throw new \InvalidArgumentException('CSRF token length not set or too short.');
		}
	
		if (function_exists('random_bytes')) {
			$randomBytes = random_bytes($length);
		}
	
		else if (function_exists('mcrypt_create_iv')) {
			$randomBytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
		}

		else if (function_exists('openssl_random_pseudo_bytes')) {
			$randomBytes = openssl_random_pseudo_bytes($length);
		}

		else {
			throw new \Exception('No suitable function for generating a CSRF token available.');
		}

		return rtrim(strtr(base64_encode($randomBytes), '+/', '-_'), '=');

	}
704623
public function generateCode($length = 16)
    {
        $lookup = implode('', array_keys($this->getLookup()));
        $code = '';

        try {
            for ($i = 0; $i < $length; $i++) {
                $code .= $lookup[random_int(0, strlen($lookup) - 1)];
            }
        } catch(\Exception $e) {
            throw $e; //return the exception to implementors of gauth so they can handle it
        }


        return $code;
    }
1044960
public function createSecret(int $length = null):string{
		$length = intval($length ?? $this->options->secret_length);

		// ~ 80 to 640 bits
		if($length < 16){
			throw new AuthenticatorException('Invalid secret length: '.$length);
		}

		$this->secret = random_bytes($length);

		return $this->getSecret();
	}
1054996
private function generateToken($length = 6)
    {
        $token = Yii::$app->security->generateRandomString($length);
        $replace = array_rand(range(2, 9));
        return str_replace(['-', '_', 'l', 1], $replace, strtolower($token));
    }
398892
protected function _createId($length = 32)
	{
		$id = '';

		while (strlen($id) < $length)  {
			$id .= mt_rand(0, mt_getrandmax());
		}

		$id	= md5(uniqid($id, true));

		return $id;
	}
157186
public function generateHardReadableString($length)
    {
        try {
            return $this->driverInstance->generateHardReadableString($length);
        } catch (\Exception $e) {
            throw new CryptException($e->getMessage());
        }
    }
107398
protected function generateHash($length)
    {
        $token = '';
        for ($i = 0; $i < $length; ++ $i) {
            $rand = mt_rand(0, 35);
            
            if ($rand < 26) {
                $char = chr(ord('a') + $rand);
            } else {
                $char = chr(ord('0') + $rand - 26);
            }
            
            $token .= $char;
        }
        return $token;
    }
968155
private function getRandomKey($length = self::DEFAULT_RANDOM_BYTE_LENGTH)
    {
        $key = '';
        $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $saltLength = strlen($salt);

        // Build the random key.
        for ($i = 0; $i < $length; $i++) {
            $key .= $salt[mt_rand(0, $saltLength - 1)];
        }

        return $key;
    }