Create a token-string

@param   integer  $length  Length of string

@return  string  Generated token

@since   1.0
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);
	}
924413
public static function generateNonce($length = 30)
    {
        $random = null;
        if ($length < 1) {
            throw new Exception('$length must be greater than 0');
        }

        // Use the new PHP 7 random_bytes call, if available
        if (!$random && function_exists('random_bytes')) {
            $random = random_bytes($length);
        }

        // Get some random binary data from open ssl, if available
        if (!$random && function_exists('openssl_random_pseudo_bytes')) {
            $random = openssl_random_pseudo_bytes($length);
        }

        // Fallback to /dev/urandom
        if (!$random && is_readable('/dev/urandom')) {
            if (($handle = @fopen('/dev/urandom', 'rb')) !== false) {
                $random = @fread($handle, $length);
                @fclose($handle);
            }
        }

        // Fallback if no random bytes were found
        if (!$random) {
            $random = microtime();

            for ($i = 0; $i < 1000; $i += $length) {
                $random = sha1(microtime() . $random);
            }
        }

        // Convert to base64
        $random = base64_encode($random);

        // Replace unsafe chars
        $random = strtr($random, '+/', '-_');
        $random = str_replace('=', '', $random);

        // Truncate the string to specified lengh
        $random = substr($random, 0, $length);

        return $random;
    }
44373
public function randomString($length = 32)
        {
            $string     = "";
            $possible   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_+=";

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

            return $string;
        }
659251
public static function getUniqueToken(int $length = 40): string
    {
        $token        = '';
        $codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
        $codeAlphabet .= '0123456789';
        $max          = strlen($codeAlphabet); // edited

        for ($i = 0; $i < $length; $i++) {
            $token .= $codeAlphabet[self::getSecureRandomInt(0, $max - 1)];
        }

        return $token;
    }
653185
protected function generateNonce( $length = 32 )
    {
        $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $result = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            $result .= $chars[mt_rand( 0, strlen( $chars ) - 1 )];
        }

        return $result;
    }
995354
private function generateToken($length)
    {
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';

        $useChars = array();
        // select some random chars:
        for ($i = 0; $i < $length; $i++) {
            $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];
        }
        // Add numbers
        array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));
        shuffle($useChars);
        $randomString = trim(implode('', $useChars));
        $randomString = substr($randomString, 0, self::TOKEN_LENGHT);

        return base64_encode($randomString);
    }
200507
public function createKey($length = 16)
    {
        if (function_exists('random_bytes')) {
            try {
                return random_bytes((int) $length);
            }
            catch (\Exception $e) {
                return FALSE;
            }
        } elseif (defined('MCRYPT_DEV_URANDOM')) {
            return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        }
        $isSecure = NULL;
        $key      = openssl_random_pseudo_bytes($length, $isSecure);

        return ($isSecure === TRUE) ? $key : FALSE;
    }
630161
private static function createPronounceable($length) {

		$retVal = '';

		$v = [
			'a', 'e', 'i', 'o',
			'u', 'ae', 'ou', 'io',
			'ea', 'ou', 'ia', 'ai',
			'ei'
		];

		$c = [
			'b', 'c', 'd', 'g',
			'h', 'j', 'k', 'l',
			'm', 'n', 'p', 'r',
			's', 't', 'u', 'v',
			'w', 'z', 'tr', 'cr',
			'fr', 'dr', 'wr', 'pr',
			'th', 'ch', 'ph', 'st',
			'sl', 'cl', 'br', 'sw'
		];

		$lenV = count($v) - 1;
		$lenC = count($c) - 1;

		// use the cryptographically secure random number generator
		
		if(function_exists('random_int')) {

			for ($i = 0; $i < $length; ++$i) {
				$retVal .= $c[random_int(0, $lenC)] . $v[random_int(0, $lenV)];
			}
				
		}

		// use the less secure fallback vor older PHP versions

		else {

			for ($i = 0; $i < $length; ++$i) {
				$retVal .= $c[mt_rand(0, $lenC)] . $v[mt_rand(0, $lenV)];
			}

		}

		return substr($retVal, 0, $length);
	}
490877
public static function generatePassphrase($length = 10){

		$recursion = 0;

		$generate = function($length){

			for($i = 0; $i < $length; $i++){

				$key = rand(0, count(self::$_passphrase_chars) - 1);
				/** @noinspection PhpUndefinedVariableInspection */
				$passphrase .= self::$_passphrase_chars[$key];
			}

			/** @noinspection PhpUndefinedVariableInspection */
			return $passphrase;
		};

		$passphrase = $generate($length);

		// Check if pass-phrase is secure enough

		$secure_pattern = "/^(?=.\{$length\})(?=(.*[A-Za-z].*){2,})(?=(.*[0-9].*){2,})(?=(.*\W.*){1,}).*$/";

		while(!preg_match($secure_pattern, $passphrase)){

			$passphrase = $generate($length);

			// We seem to be stuck; abort and go for last generated passphrase

			if($recursion > 10){

				ExceptionHandler::notice('Text::generatePassphrase() seems to be stuck, aborting');
				break;
			}
		}

		return $passphrase;
	}
1083447
static public function EncodeLength($length)
    {
        self::CheckMessageLength($length);

        $string = "";
        do{
            $digit = $length % 0x80;
            $length = $length >> 7;
            // if there are more digits to encode, set the top bit of this digit
            if ( $length > 0 ) $digit = ($digit | 0x80);
            $string .= chr($digit);
        } while ( $length > 0 );

        return $string;
    }
533706
private function createPasswordSalt( $length = 22 )
    {
        static $chars = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

        if ( function_exists( 'openssl_random_pseudo_bytes' ) &&
             ( version_compare( PHP_VERSION, '5.3.4' ) >= 0 ||
               strtoupper( substr( PHP_OS, 0, 3 ) ) !== 'WIN' ) )
        {
            $bytes = openssl_random_pseudo_bytes( $length, $usable );

            if ( true !== $usable )
            {
                $bytes = null;
            }
        }

        if ( empty( $bytes ) &&
             function_exists( 'mcrypt_create_iv' ) &&
             ( version_compare( PHP_VERSION, '5.3.7' ) >= 0 ||
               strtoupper( substr( PHP_OS, 0, 3 ) ) !== 'WIN' ) )
        {
            $bytes = mcrypt_create_iv( $length, MCRYPT_DEV_URANDOM );

            if ( empty( $bytes ) || strlen( $bytes ) < $length )
            {
                $bytes = null;
            }
        }

        if ( empty( $bytes ) )
        {
            $bytes = '';

            for ( $i = 0; $i < $length; ++$i )
            {
                $bytes .= chr( mt_rand( 0, 255 ) );
            }
        }

        $pos  = 0;
        $salt = '';
        $clen = strlen( $chars );

        for ( $i = 0; $i < $length; ++$i )
        {
            $pos = ( $pos + ord( $bytes[$i] ) ) % $clen;
            $salt .= $chars[$pos];
        }

        return $salt;
    }
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;
    }
1106661
public static function generateRandom($length = 10)
    {
        $max = ceil($length / 40);
        $random = '';
        for ($i = 0; $i < $max; $i++) {
            $random .= sha1(microtime(true) . mt_rand(10000, 90000));
        }

        return substr($random, 0, $length);
    }
708526
private function _generateCSRFToken($length=32)
    {
        $randUtils = new RandomUtils();
        $randToken = $randUtils->getBytes($length, true);
        return base64_encode($randToken);
    }
374283
protected function randomString($length)
	{
		$seed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijqlmnopqrtsuvwxyz0123456789';
		$max = strlen($seed) - 1;
		$string = '';
		for ($i = 0; $i < $length; ++$i) {
			$string .= $seed{intval(mt_rand(0.0, $max))};
		}

		return $string;
	}
1232624
public static function GenerateRandomString(int $length = 20)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
649381
public static function generateRandomAlphaNumericString($length) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $size = strlen($chars);

        $randomChars = array();
        for ($i = 0; $i < $length; $i++) {
            $randomChars[] = $chars[mt_rand(0, $size - 1)];
        }

        return implode('', $randomChars);
    }