Generates a security token
@param int $length the expected token length
@return string the token
@throws \common_Exception
353360
protected function generate($length = 40)
    {
        try {
            return bin2hex(random_bytes($length / 2));
        } catch (\TypeError $e) {
            // This is okay, so long as `Error` is caught before `Exception`.
            throw new \common_Exception("An unexpected error has occurred while trying to generate a security token", 0, $e);
        } catch (\Error $e) {
            // This is required, if you do not need to do anything just rethrow.
            throw new \common_Exception("An unexpected error has occurred while trying to generate a security token", 0, $e);
        } catch (\Exception $e) {
            throw new \common_Exception("Could not generate a security token. Is our OS secure?", 0, $e);
        }
    }
634449
public function salt(int $length = 32): string {
    try {
      return random_bytes($length);
    } catch(Exception $e) {
      throw new RuntimeException('Could not generate crytographically secure salt', 0, $e);
    }
  }
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), '+/', '-_'), '=');

	}
117537
public function generateHardReadableString($length)
    {
        return $this->generateRandomString($length, self::SYMBOLS . self::CHARS . self::NUMBERS . self::SYMBOLS);
    }
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();
	}
743789
private function randomToken($length = 32)
    {
        if (!isset($length) || intval($length) <= 8) {
            $length = 32;
        }
        if (function_exists('random_bytes')) {
            return random_bytes($length);
        }
        if (function_exists('mcrypt_create_iv')) {
            return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        }
        if (function_exists('openssl_random_pseudo_bytes')) {
            return openssl_random_pseudo_bytes($length);
        }

        return @crypt(uniqid());
    }
232181
protected function generateNonce($length = 32)
    {
        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';

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

        return $nonce;
    }
466114
protected function generate_key( $length = 64 ) {
		$unique  = md5( microtime() . rand() );
		$prepend = $this->identifier . '_batch_';

		return substr( $prepend . $unique, 0, $length );
	}
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);
    }
945670
protected function generateUniqueIdentifier($length = 40)
    {
        try {
            return bin2hex(random_bytes($length));
            // @codeCoverageIgnoreStart
        } catch (TypeError $e) {
            throw OAuthServerException::serverError('An unexpected error has occurred', $e);
        } catch (Error $e) {
            throw OAuthServerException::serverError('An unexpected error has occurred', $e);
        } catch (Exception $e) {
            // If you get this message, the CSPRNG failed hard.
            throw OAuthServerException::serverError('Could not generate a random string', $e);
        }
        // @codeCoverageIgnoreEnd
    }
40063
public function string(int $length = 8)
    {
        $list = 'abcdefghijklmnopqrstuvwxyz';
        $return = '';
        for ($i = 0; $i < $length; $i++) {
            $return .= $list[rand(0, strlen($list) - 1)];
        }

        return $return;
    }
3831
public function generateToken($token_length=30, $prefix='') {
        $token_length = $token_length - strlen($prefix);
        if ($token_length < 0) { throw new Exception("Prefix is too long", 1); }
        
        $token = "";
        while (($len = strlen($token)) < $token_length) {
            $remaining_len = $token_length - $len;
            $bytes = random_bytes($remaining_len);
            $token .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $remaining_len);
        }

        return $prefix.$token;
    }
255413
public function generateAlphaNumToken($length)
    {
        $characters = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');

        srand((float)microtime() * 1000000);

        $token = '';

        do
        {
            shuffle($characters);
            $token .= $characters[mt_rand(0, (count($characters) - 1))];
        } while (strlen($token) < $length);

        return $token;
    }
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);
    }
360813
public static function generator( $length = 32 ) {
		$out = Hash::base64( Route::HTTP_HOST() . random_bytes( $length ), 1 );

		return $out;
	}
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;
	}
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;
    }