Generate a secure passphrase.

@param integer $length
@return string
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;
	}
375011
public static function randAlphabet($length)
    {
        if ($length < 0) $length = 1;
        if ($length > 64) $length = 64;
        $code = [];
        for ($i = 0; $i < $length; $i++) {
            $code[$i] = self::$alphaCodeSet[mt_rand(0, strlen(self::$alphaCodeSet) - 1)];
        }
        return implode("", $code);
    }
674562
protected function keygen($length)
	{
		$key = '';
		$chars = array_merge(range(0, 9), range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9));
		shuffle($chars);

		$chars = str_shuffle(str_rot13(join('', $chars)));
		$split = intval(ceil($length / 5));
		$size = strlen($chars);

		$splitSize = ceil($size / $split);
		$chunkSize = 5 + $splitSize + mt_rand(1, 5);
		$chunkArray = array();

		$chars = str_shuffle(str_repeat($chars, 2));
		$size = strlen($chars);

		while ($split != 0) {
			$strip = substr($chars, mt_rand(0, $size - $chunkSize), $chunkSize);
			array_push($chunkArray, strrev($strip));
			$split--;
		}

		foreach ($chunkArray as $set) {
			$modulus = ($length - strlen($key)) % 5;
			$adjusted = ($modulus > 0) ? $modulus : 5;

			$key .= substr($set, mt_rand(0, strlen($set) - $adjusted), $adjusted);
		}

		return str_rot13(str_shuffle($key));
	}
58141
public function getRandomString(int $length = 64): string
    {
        $chars  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
        $random = '';

        for ($i = 0; $i < $length; $i++) {
            $random .= substr($chars, random_int(0, strlen($chars) - 1), 1);
        }

        return $random;
    }
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);
    }
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);
	}
300996
public function generatePassword($length = 8, $options = []) {
		$options = $this->_passwordDictionary($options);
		$password = '';

		srand((int)microtime() * 1000000);
		for ($i = 0; $i < $length; $i++) {
			$password .=
				$options['cons'][mt_rand(0, count($options['cons']) - 1)] .
				$options['vowels'][mt_rand(0, count($options['vowels']) - 1)];
		}

		return substr($password, 0, $length);
	}
129506
public function getByte($length)
    {
        if ($length === 0) {
            return '';
        }

        if (function_exists('random_bytes')) {
            /** @noinspection PhpUnhandledExceptionInspection */
            return random_bytes($length);
        } elseif (function_exists('openssl_random_pseudo_bytes')) {
            /** @noinspection CryptographicallySecureRandomnessInspection */
            return openssl_random_pseudo_bytes($length);
        } elseif (file_exists('/dev/urandom')) {
            $handle = fopen('/dev/urandom', 'rb');

            stream_set_read_buffer($handle, 0);
            $r = fread($handle, $length);
            fclose($handle);

            return $r;
        } else {
            $r = '';

            do {
                $r .= pack('L', mt_rand());
            } while (strlen($r) < $length);

            return substr($r, 0, $length);
        }
    }
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;
    }
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;
        }
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;
    }
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;
    }
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);
    }
1306662
public static function generate( $length = 16 ) {
		if ( function_exists( 'random_bytes' ) ) {
			return bin2hex( random_bytes( $length ) );
		} elseif ( function_exists( 'openssl_random_pseudo_bytes' ) ) {
			return bin2hex( openssl_random_pseudo_bytes( $length ) );
		} else {
			$id = uniqid();
			$hash = '';
			$str_len = strlen( $id );
			$limit = floor( $length * 2 / $str_len );
			for ( $i = 0; $i < $limit; $i++ ) {
				$hash .= $id;
			}
			$remainder = strlen( $id ) / ( $length * 2 );
			if ( $remainder ) {
				$hash .= substr( $id, 0, $remainder );
			}
			return $hash;
		}
	}
1226170
public static function random_password($length = 8)
	{
		$genpass = '';
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";

		srand((double)microtime()*1000000);

		$i = 0;

		while ($i < $length)
		{
			$num = rand() % 33;
			$tmp = substr($salt, $num, 1);
			$genpass = $genpass . $tmp;
			$i++;
		}

		return $genpass;
	}
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;
    }
1244213
public static function generateToken($length = 32, $strong = true)
    {
        $token = base64_encode(openssl_random_pseudo_bytes($length + 10, $strong));
        $token = str_replace(['/'], '', $token);

        return substr($token, 0, $length);
    }