Get a randomly generated secure token

@param  int $length Length of the string to return

@throws \Kunnu\Dropbox\Exceptions\DropboxClientException

@return string
183048
public function generateString($length)
    {
        //Create Binary String
        $binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);

        //Unable to create binary string
        if ($binaryString === false) {
            throw new DropboxClientException(
                static::ERROR_MESSAGE .
                'mcrypt_create_iv() returned an error.'
                );
        }

        //Convert binary to hex
        return $this->binToHex($binaryString, $length);
    }
1184451
public static function generateRandomHandle($length = 32)
    {
        do {
            $handle = substr(md5(mt_rand(0, mt_getrandmax())), 0, $length);
        } while (static::getByField(static::$handleField, $handle));

        return $handle;
    }
519732
public static function randomString($length)
    {
        $key = '';
        $keys = array_merge(range('a', 'z'));

        for ($i = 0; $i < $length; ++$i) {
            $key .= $keys[array_rand($keys)];
        }

        return $key;
    }
1294007
public static function random($length) {
        $res = '';
        for ($i = 0; $i < $length; $i++) {
            $res .= chr(mt_rand(33, 126));
        }
        return $res;
    }
599538
private static function generateRandomString($length = 10)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';

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

        return $string;
    }
1224865
public static function generate(string $token = null, int $length = null): Token
    {
        if ($token == null) {
            if ($length == null) {
                $length = 64;
            }

            // generate token here
            if (function_exists('openssl_random_pseudo_bytes')) {
                $token = bin2hex(openssl_random_pseudo_bytes($length));

                if (strlen($token) > $length) {
                    // we have a string, now, take some random part there
                    $token = substr($token, rand(0, strlen($token) - $length), $length);
                }
            } else {
                // a fallback if openssl_random_pseudo_bytes is not accessible for some reason
                $someString = time() . '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' . Application::getKey();
                $token = substr(str_shuffle($someString), 0, $length);
            }
        }

        $config = Application::getConfig('session');

        $cookieName = static::getCookieName();
        if (is_string($cookieName) && strlen($cookieName) > 0) {
            $cookie = Cookie::rawSet($cookieName, $token, 0, '/', $config->get('domain', ''), $config->get('cookie_secure', false), false);
        } else {
            $cookie = null;
        }

        $token = new Token($token, $cookie);
        static::$token = $token;

        $sessionKeyName = static::getSessionKeyName();
        if (is_string($sessionKeyName) && strlen($sessionKeyName) > 0) {
            Session::set($sessionKeyName, $token);
        }

        return $token;
    }
1146271
public function generateSecret($length = 10)
	{
		$strong_secret = false;

		// Try to get $crypto_strong to evaluate to true. Give it 5 tries.
		for ($i = 0; $i < 5; $i++)
		{
			$secret = openssl_random_pseudo_bytes($length, $strong_secret);

			if ($strong_secret === true)
			{
				return $this->base32->encode($secret);
			}
		}

		return '';
	}
999316
public static function random($length = null)
    {
        if ($length === null) {
            $length = Configure::read('Muffin/Tokenize.length', self::DEFAULT_LENGTH);
        }

        return bin2hex(Security::randomBytes($length / 2));
    }
686260
public static function random(int $length): string
    {
        $byteLength = ($length < 10) ? 10 : $length;
        $chars = base64_encode(random_bytes($byteLength));
        $chars = str_replace(["+", "/", "="], "", $chars);
        return substr($chars, 0, $length);
    }
514294
protected function _createToken($length = 32)
	{
		static $chars = '0123456789abcdef';
		$max = strlen($chars) - 1;
		$token = '';
		$name = session_name();

		for ($i = 0; $i < $length; ++$i)
		{
			$token .= $chars[(rand(0, $max))];
		}

		return md5($token . $name);
	}
1170252
public static function random2($length = 16)
        {
            if (function_exists('openssl_random_pseudo_bytes')) {
                $bytes = openssl_random_pseudo_bytes($length * 2);

                if ($bytes === false) {
                    throw new Exception('Unable to generate random string.');
                }

                return substr(repl(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
            }

            return static::quickRandom($length);
        }
713586
protected function keygen($length)
	{
		$chars = str_shuffle('3759402687094152031368921');
		$chars = str_shuffle(str_repeat($chars, ceil($length / strlen($chars))));
		
		return strrev(str_shuffle(substr($chars, mt_rand(0, (strlen($chars) - $length - 1)), $length)));
	}
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));
    }
716282
public static function randomHex(int $length = 128): string
    {
        $bytes = ceil($length / 2);
        $hex = bin2hex(self::randomBytes($bytes));
        return $hex;
    }
604866
public function generateRandomString(int $length = 32): string
    {
        if ($length < 1) {
            throw new \RuntimeException('First parameter ($length) must be greater than 0');
        }

        $bytes = $this->generateRandomKey($length);
        return \substr(StringHelper::base64UrlEncode($bytes), 0, $length);
    }
1238819
public static function insecureRandomBytes($length)
    {
        $length *= 2;

        $bytes = '';
        $byteLength = 0;
        while ($byteLength < $length) {
            $bytes .= static::hash(Text::uuid() . uniqid(mt_rand(), true), 'sha512', true);
            $byteLength = strlen($bytes);
        }
        $bytes = substr($bytes, 0, $length);

        return pack('H*', $bytes);
    }
1021750
protected function createToken($length = 32)
    {
        static $chars = '0123456789abcdef';
        $max = strlen($chars) - 1;
        $token = '';
        $name = $this->getName();

        for ($i = 0; $i < $length; ++$i) {
            $token .= $chars[(mt_rand(0, $max))];
        }

        return md5($token . $name);
    }